A list makes a view its tables hold once and copies its descriptor (VICTORY_LIST_VIEWS=0: for every table); a draw's tables are gone through without memory of the heap

The recording job the frame waits for last (24 of 65 ms, timeline waits-2) is
2,500 indirect dispatches, each with a table of its own that differs in a
constant buffer. VICTORY_AB can turn VICTORY_MAP_THREADS (8 or 1) too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 09:03:29 -06:00
1 parent a1bdba4cf9
commit 4e77a8795b
5 files changed
+326 -29

No files matched your search

+10
View File
@@ -1126,6 +1126,16 @@ fn view_at(i: u32, generation: u32) -> Option<View> {
}
}
/// The words of the record at `handle`, if they are a view's.
pub(crate) fn record_at(handle: u64) -> Option<[u32; 3]> {
if handle == 0 {
return None;
}
// SAFETY: the handle is a descriptor slot the title passes back.
let words = unsafe { (handle as *const [u32; 3]).read_unaligned() };
(words[0] == VIEW_TAG).then_some(words)
}
/// The view recorded at `handle`, if one is (and its resource is alive).
pub(crate) fn view(handle: u64) -> Option<View> {
if handle == 0 {
+159
View File
@@ -2129,6 +2129,68 @@ impl HostHeap {
self.keep(index, kept.flatten());
}
/// Makes slot `index` (a dynamic one) the view the record `words` names,
/// from this thread's descriptor of it if the recording `recording` has
/// made one, else as [`HostHeap::make_at`] does, and keeps that
/// ([`ListViews`]). What is known of the record in this recording;
/// `None` (the slot is not made): not a view kept so.
pub(crate) fn make_listed(&self, recording: u64, words: [u32; 3], index: u32) -> Option<Listed> {
if self.sampler {
return None;
}
LIST_VIEWS.with(|views| {
let mut views = views.borrow_mut();
if views.is_none() {
*views = Some(ListViews::new()?);
}
let views = views.as_mut()?;
if views.recording != recording {
views.recording = recording;
views.made.clear();
}
let twins = self.twins.get(index as usize).is_some();
if !views.made.contains_key(&words) {
let view = super::view_at(words[1], words[2])?;
let at = 2 * views.made.len() as u32;
// (A constant buffer's bytes are copied for each draw.)
if at >= ListViews::SLOTS || matches!(view, View::Constant(..)) {
return None;
}
let twin = twinned(&view);
let kept = make_twinned(&view, views.slot(at), Some(views.slot(at + 1)))?;
let listed = Listed {
kind: crate::draw::view_kind(&view),
texture: is_texture_view(&view),
ready: (0, 0, false),
tabled: None,
};
views.made.insert(words, ListView { view, at, twin, kept, listed });
}
let made = &views.made[&words];
self.copy_from(index, views.slot(made.at));
if made.twin && twins {
self.copy_from(self.raw + index, views.slot(made.at + 1));
self.twins[index as usize].store(true, std::sync::atomic::Ordering::Relaxed);
} else {
self.clear_twin(index);
}
self.keep(index, made.kept.clone());
Some(made.listed)
})
}
/// As [`HostHeap::copy_to`], from the slot `from`, which the card does not
/// see.
fn copy_from(&self, to: u32, from: D3D12_CPU_DESCRIPTOR_HANDLE) {
#[cfg(feature = "diagnostics")]
if let Some(written) = self.written.get(to as usize) {
written.store(crate::host::coarse_ms() as u32, std::sync::atomic::Ordering::Relaxed);
}
let Some(host) = host() else { return };
// SAFETY: a slot of our heap, and one of this thread's heap.
unsafe { host.device.CopyDescriptorsSimple(1, self.cpu(to), from, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) };
}
/// Makes slot `index` read nothing: a null view, or a plain sampler.
pub(crate) fn make_null(&self, index: u32) {
if self.sampler {
@@ -4664,3 +4726,100 @@ mod tests {
assert_eq!(heap.dynamic(2), None);
}
}
/// What is known of a record a list's tables hold, in that recording of the
/// list ([`HostHeap::make_listed`]).
#[derive(Clone, Copy)]
pub(crate) struct Listed {
pub(crate) kind: Option<victory_shader::native::ResourceType>,
pub(crate) texture: bool,
/// Read at this epoch of the list ([`crate::host::HostList::epoch`]) with
/// this depth target tested: up to date; and whether it is that target's.
pub(crate) ready: (u64, usize, bool),
/// The title's slot last noted for it, and whether the draw read it.
pub(crate) tabled: Option<(u32, bool)>,
}
struct ListView {
view: View,
/// Its descriptor's slot in [`ListViews`]; its raw twin's is the next.
at: u32,
twin: bool,
kept: Kept,
listed: Listed,
}
/// The descriptors a thread made for one recording of a list, by the record
/// they were made from, in a heap the card does not see.
///
/// The title's culling and lighting dispatches each bind a table of their own
/// that differs in one constant buffer, and the same thirty views beside it:
/// each was found, checked, made and brought up to date anew for every
/// dispatch, some 2,500 indirect dispatches in the one recording job the
/// frame waits for last (timeline waits-2: 24 of 65 ms). Here a list makes a
/// view once, as it makes a table once (`crate::draw::State::tables`), and
/// copies its descriptor.
struct ListViews {
recording: u64,
heap: ID3D12DescriptorHeap,
step: usize,
start: usize,
made: std::collections::HashMap<[u32; 3], ListView, std::hash::BuildHasherDefault<crate::host::AddressHash>>,
}
impl ListViews {
const SLOTS: u32 = 8192;
fn new() -> Option<ListViews> {
let host = host()?;
// SAFETY: plain object creation.
unsafe {
let heap: ID3D12DescriptorHeap = host
.device
.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC {
Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
NumDescriptors: Self::SLOTS,
..Default::default()
})
.ok()?;
let step = host.device.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) as usize;
let start = heap.GetCPUDescriptorHandleForHeapStart().ptr;
Some(ListViews { recording: 0, heap, step, start, made: Default::default() })
}
}
fn slot(&self, i: u32) -> D3D12_CPU_DESCRIPTOR_HANDLE {
debug_assert!(i < Self::SLOTS && !windows::core::Interface::as_raw(&self.heap).is_null());
D3D12_CPU_DESCRIPTOR_HANDLE { ptr: self.start + i as usize * self.step }
}
}
thread_local! {
static LIST_VIEWS: std::cell::RefCell<Option<ListViews>> = const { std::cell::RefCell::new(None) };
}
/// The view the record `words` names, as this thread made it for `recording`.
pub(crate) fn listed_view(recording: u64, words: [u32; 3]) -> Option<View> {
LIST_VIEWS.with(|views| {
let views = views.borrow();
let views = views.as_ref().filter(|v| v.recording == recording)?;
views.made.get(&words).map(|m| m.view.clone())
})
}
/// Notes what is known of the record `words` in `recording`.
pub(crate) fn note_listed(recording: u64, words: [u32; 3], listed: Listed) {
LIST_VIEWS.with(|views| {
if let Some(views) = views.borrow_mut().as_mut().filter(|v| v.recording == recording)
&& let Some(made) = views.made.get_mut(&words)
{
made.listed = listed;
}
});
}
/// Whether a list makes a view its tables hold once (`VICTORY_LIST_VIEWS=0`:
/// for every table).
pub(crate) fn list_views() -> bool {
crate::toggled!("VICTORY_LIST_VIEWS")
}
+145 -29
View File
@@ -341,6 +341,9 @@ pub(crate) struct State {
root_uav_slots: std::collections::HashMap<(usize, u64), RootSlot>,
/// The list ran and its dynamic slots were given back.
tables_gone: bool,
/// This recording, for [`crate::descriptor::host::HostHeap::make_listed`]
/// (0: it has none yet).
recording: u64,
}
impl Drop for State {
@@ -371,6 +374,7 @@ impl State {
crate::descriptor::release_dynamic(heap, first, n);
}
self.fresh_tables.clear();
self.recording = 0;
self.root_srv_slots.clear();
self.root_uav_slots.clear();
self.tables_gone = true;
@@ -1463,7 +1467,16 @@ impl State {
None
};
if let Some((param, dwords)) = stage.user_data {
let mut values = vec![0u32; dwords as usize];
// (A root signature holds 64 dwords.)
let mut room = [0u32; 64];
let mut more = Vec::new();
let values: &mut [u32] = match room.get_mut(..dwords as usize) {
Some(values) => values,
None => {
more.resize(dwords as usize, 0);
&mut more
}
};
for u in &t.host.user_data {
if let RootType::Other(kind) = u.root {
values[u.index as usize] =
@@ -1481,7 +1494,7 @@ impl State {
#[cfg(feature = "diagnostics")]
{
self.user.clear();
self.user.extend_from_slice(&values);
self.user.extend_from_slice(values);
}
// SAFETY: an open list; `values` covers the parameter.
unsafe {
@@ -2934,14 +2947,14 @@ impl State {
used: &[UsedResource],
indexed: &[RootIndexedView],
compute: bool,
) -> Vec<(u8, u32)> {
) -> Few<(u8, u32), 16> {
let tables = |u: &&UsedResource| u.root == RootType::Table;
let mut fresh: Vec<u8> = used
.iter()
.filter(tables)
.filter(|u| u.kind == ResourceType::Cbv)
.map(|u| u.slot)
.collect();
let mut fresh: Few<u8, 16> = Few::default();
for u in used.iter().filter(tables).filter(|u| u.kind == ResourceType::Cbv) {
if !fresh.contains(&u.slot) {
fresh.push(u.slot);
}
}
// Tables reaching past a host heap smaller than the title's (the
// host's limit, 1,000,000 views on NVIDIA) are made afresh too: the
// title's slots there have no host slot of their own.
@@ -2964,22 +2977,22 @@ impl State {
}
}
fresh.sort_unstable();
fresh.dedup();
for u in used.iter().filter(tables) {
if !fresh.contains(&u.slot) {
self.table_entry(list, u, compute);
}
}
let mut made = Vec::new();
for slot in fresh {
let mut made = Few::default();
for &slot in fresh.iter() {
let Some(&Root::Table(table)) = self.roots_ref(compute).get(usize::from(slot)) else {
continue;
};
let entries: Vec<&UsedResource> = used
.iter()
.filter(tables)
.filter(|u| u.slot == slot)
.collect();
// (Their places among `used`.)
let mut places: Few<u16, 32> = Few::default();
for (i, _) in used.iter().enumerate().filter(|(_, u)| tables(u) && u.slot == slot) {
places.push(i as u16);
}
let entries = || places.iter().map(|&i| &used[usize::from(i)]);
let Some(heap) = self.heap_of(table) else {
continue;
};
@@ -3005,8 +3018,7 @@ impl State {
.collect();
selected.sort_unstable();
selected.dedup();
let n = entries
.iter()
let n = entries()
.map(|u| u.table_offset_dwords / 8)
.chain(selected.last().copied())
.max()
@@ -3035,7 +3047,7 @@ impl State {
let first = known.first;
// This reader had it brought up to date, and nothing in the
// list changed since: nothing to do again.
let once = ready_once() && selected.is_empty() && entries.iter().all(|u| u.kind != ResourceType::Uav);
let once = ready_once() && selected.is_empty() && entries().all(|u| u.kind != ResourceType::Uav);
if once
&& known.epoch == list.epoch()
&& let Some((_, depth)) = known.ready.iter().find(|(r, _)| *r == reader)
@@ -3048,14 +3060,14 @@ impl State {
}
let depth_before = TESTED_DEPTH.get();
TESTED_DEPTH.set((depth_before.0, false));
for u in entries.iter().filter(|u| u.kind != ResourceType::Cbv) {
for u in entries().filter(|u| u.kind != ResourceType::Cbv) {
let handle = table + u64::from(u.table_offset_dwords) * 4;
if let Some(view) = crate::descriptor::view(handle) {
readable(list, u.kind, &view);
}
}
for k in selected {
if entries.iter().any(|u| u.table_offset_dwords / 8 == k) {
if entries().any(|u| u.table_offset_dwords / 8 == k) {
continue;
}
let handle = table + u64::from(k) * 32;
@@ -3098,15 +3110,63 @@ impl State {
// which may lie over slots shaders pick by index, only the ones
// the shader names.)
if let Some(title) = title_first {
for k in (0..n).filter(|&k| n <= 64 || entries.iter().any(|u| u.table_offset_dwords / 8 == k)) {
for k in (0..n).filter(|&k| n <= 64 || entries().any(|u| u.table_offset_dwords / 8 == k)) {
host.slot_tabled(title + k);
}
}
let list_views = crate::descriptor::host::list_views();
if self.recording == 0 {
static NEXT: AtomicU64 = AtomicU64::new(1);
self.recording = NEXT.fetch_add(1, Ordering::Relaxed);
}
let recording = self.recording;
// The whole range: a shader may index past the entries its
// used-resource list names (descriptor arrays).
for k in 0..n {
let index = first + k;
let used = entries.iter().find(|u| u.table_offset_dwords / 8 == k);
// A view this list has made before: its descriptor copied,
// and nothing more if nothing changed in the list since.
if list_views
&& let Some(words) = crate::descriptor::record_at(table + u64::from(k) * 32)
&& let Some(mut listed) = host.make_listed(recording, words, index)
{
let kind = entries()
.find(|u| u.table_offset_dwords / 8 == k)
.map(|u| u.kind)
.or_else(|| selected.binary_search(&k).ok().and(listed.kind));
let was = (listed.ready, listed.tabled);
if let Some(kind) = kind {
let tested = TESTED_DEPTH.get();
if kind == ResourceType::Srv && listed.ready.0 == list.epoch() && listed.ready.1 == tested.0 {
if listed.ready.2 {
TESTED_DEPTH.set((tested.0, true));
}
} else if let Some(view) = crate::descriptor::host::listed_view(recording, words) {
TESTED_DEPTH.set((tested.0, false));
readable(list, kind, &view);
let depth = TESTED_DEPTH.get().1;
TESTED_DEPTH.set((tested.0, tested.1 || depth));
if kind == ResourceType::Srv {
listed.ready = (list.epoch(), tested.0, depth);
}
}
}
if let Some(title) = title_first
&& listed.texture
&& listed.tabled != Some((title + k, kind.is_some()))
{
listed.tabled = Some((title + k, kind.is_some()));
host.texture_slot_tabled(title + k, kind.is_some());
if kind.is_some() {
host.mirror(title + k, table + u64::from(k) * 32, true);
}
}
if was != (listed.ready, listed.tabled) {
crate::descriptor::host::note_listed(recording, words, listed);
}
continue;
}
let used = entries().find(|u| u.table_offset_dwords / 8 == k);
match crate::descriptor::view(table + u64::from(k) * 32) {
Some(View::Constant(address, size)) => {
let Some((buffer, gpu, to)) = list.scratch_held(u64::from(size)) else {
@@ -3703,7 +3763,7 @@ fn host_signature(kind: u32, stride: u32) -> Option<ID3D12CommandSignature> {
/// Gets what a shader reads or writes through a view ready: buffer data
/// copied, textures moved to the state they need.
fn view_kind(view: &View) -> Option<ResourceType> {
pub(crate) fn view_kind(view: &View) -> Option<ResourceType> {
match view {
View::Shader(..) | View::Placed(false, ..) => Some(ResourceType::Srv),
View::Unordered(..) | View::Placed(true, ..) => Some(ResourceType::Uav),
@@ -3739,6 +3799,48 @@ fn depth_state(texture: &ID3D12Resource, desc: Option<&D3D12_DEPTH_STENCIL_VIEW_
}
}
/// A few things, up to `N` of them without memory of the heap: what a draw's
/// tables are gone through with, some six lists a stage.
struct Few<T, const N: usize> {
few: [T; N],
len: usize,
/// All of them, if they are more.
many: Vec<T>,
}
impl<T: Copy + Default, const N: usize> Default for Few<T, N> {
fn default() -> Self {
Few { few: [T::default(); N], len: 0, many: Vec::new() }
}
}
impl<T: Copy, const N: usize> Few<T, N> {
fn push(&mut self, what: T) {
if self.many.is_empty() && self.len < N {
self.few[self.len] = what;
self.len += 1;
} else {
if self.many.is_empty() {
self.many.extend_from_slice(&self.few);
}
self.many.push(what);
}
}
}
impl<T, const N: usize> std::ops::Deref for Few<T, N> {
type Target = [T];
fn deref(&self) -> &[T] {
if self.many.is_empty() { &self.few[..self.len] } else { &self.many }
}
}
impl<T, const N: usize> std::ops::DerefMut for Few<T, N> {
fn deref_mut(&mut self) -> &mut [T] {
if self.many.is_empty() { &mut self.few[..self.len] } else { &mut self.many }
}
}
/// A table made afresh in dynamic slots, in a list ([`State::tables`]).
struct FreshTable {
first: u32,
@@ -3806,9 +3908,11 @@ fn readable(list: &HostList, kind: ResourceType, view: &View) {
let aliased = crate::resource::alias_of(*resource as This)
.is_some_and(|source| crate::texture::alias(list, *resource as This, source));
if !aliased
&& let Some(t) = crate::resource::host_resource(*resource as This)
&& crate::texture::title_written(t)
&& let Some(t) = crate::resource::host_copy(*resource as This)
&& t.flags() & 0x7 == 0
{
// (A texture the title writes: no target, depth or unordered
// access, `crate::texture::title_written`.)
// Of a texture the title placed, the levels the view shows.
let first = match (&placed, view) {
(Some(_), View::Shader(_, Some(desc))) => crate::resource::first_mip(desc),
@@ -4083,7 +4187,18 @@ mod tests {
#[cfg(test)]
mod record_hash_tests {
use super::RecordHash;
use super::{Few, RecordHash};
#[test]
fn a_few_things_are_kept_in_order_however_many() {
for n in 0..10u32 {
let mut few: Few<u32, 4> = Few::default();
(0..n).for_each(|i| few.push(n - i));
assert_eq!(few.to_vec(), (0..n).map(|i| n - i).collect::<Vec<_>>());
few.sort_unstable();
assert_eq!(few.to_vec(), (1..=n).collect::<Vec<_>>());
}
}
#[test]
fn records_that_differ_hash_apart() {
@@ -4104,7 +4219,8 @@ mod record_hash_tests {
}
}
assert!(seen.insert(of(&[])));
let zeros: Vec<u64> = (1..100).map(|n| of(&vec![[0; 3]; n])).collect();
// (One record of zeros is among the above.)
let zeros: Vec<u64> = (2..100).map(|n| of(&vec![[0; 3]; n])).collect();
assert!(zeros.iter().all(|h| seen.insert(*h)));
}
}
+7
View File
@@ -83,6 +83,13 @@ mod watch;
#[cfg(windows)]
pub use host::on_failure;
/// Whether `switch` is on now, if it is one of those `VICTORY_AB` turns on and
/// off while the title runs (`ab`).
#[cfg(all(windows, feature = "diagnostics"))]
pub fn switched(switch: &str) -> Option<bool> {
ab::pick(switch)
}
/// What the frame is at (`copystat::phase`), for the sampling profiler.
#[cfg(all(windows, feature = "diagnostics"))]
pub fn frame_phase() -> u32 {
+5
View File
@@ -1264,6 +1264,11 @@ const GROUP: usize = 64;
/// queue once at a time. In a bench (60,000 views, five threads changing
/// protections) one thread mapped 1,500 pages a second, eight 30,000.
fn side_by_side() -> usize {
// Diagnostics: 1 and 8 by turns.
#[cfg(all(windows, feature = "diagnostics"))]
if let Some(on) = victory_gpu::switched("VICTORY_MAP_THREADS") {
return if on { 8 } else { 1 };
}
static THREADS: LazyLock<usize> = LazyLock::new(|| {
std::env::var("VICTORY_MAP_THREADS").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(8).clamp(1, 32)
});