Merge day/textures

This commit is contained in:
local committed 2026-11-25 12:55:58 -06:00
commit f6e2b31ceb
20 files changed
+1887 -84

No files matched your search

+303 -13
View File
@@ -365,6 +365,13 @@ fn neutral_for(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) -> Option<ID3D12Res
static FLIPS: Mutex<Vec<Flip>> = Mutex::new(Vec::new());
/// Makes the slots whose new texture's upload has run
/// ([`HostHeap::texture_slots_flip`]); on any thread. Not if the heap is at
/// something else: the next texture pass does.
pub(crate) fn flip_now() {
let _ = super::with_view_heap(HostHeap::texture_slots_flip);
}
/// Since the start: slots made from their new view after its upload had run,
/// and those the title had written again by then.
pub(crate) static FLIPPED: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2];
@@ -754,6 +761,20 @@ pub(crate) fn remapped(at: usize, len: usize) {
ranges.push((start, end));
}
/// Ranges of memory, those that touch made one (DexNom/VIctory 921d8d3b:
/// several threads map and write, some 5,000 ranges a frame, the same
/// textures' over and over, and each was looked up).
fn join(ranges: &mut Vec<(u64, u64)>) {
ranges.sort_unstable();
ranges.dedup_by(|next, last| {
if next.0 > last.1 {
return false;
}
last.1 = last.1.max(next.1);
true
});
}
/// Frames for which a slot a draw read through a table counts as a table's
/// slot ([`HostHeap::tabled`]).
const TABLED_FRAMES: u64 = 240;
@@ -1037,6 +1058,17 @@ fn view_stride(view: &View) -> Option<u32> {
})
}
/// What else the table of strides says of a typed buffer view of an integer
/// format (`victory_shader::emit::VIEW_INTEGER`); 0 for any other view.
fn view_kind(view: &View) -> u32 {
use victory_shader::emit::{VIEW_INTEGER, VIEW_SIGNED};
match view_elements(view) {
Some((0, false, 3 | 7 | 12 | 17 | 25 | 30 | 36 | 42 | 50 | 57 | 62)) => VIEW_INTEGER,
Some((0, false, 4 | 8 | 14 | 18 | 32 | 38 | 43 | 52 | 59 | 64)) => VIEW_INTEGER | VIEW_SIGNED,
_ => 0,
}
}
/// A buffer view's structure stride, whether it is raw, and its format;
/// `None` if `view` is not a buffer view.
fn view_elements(view: &View) -> Option<(u32, bool, u32)> {
@@ -1732,10 +1764,11 @@ impl HostHeap {
pub(crate) fn texture_slots_remapped(&self, frame: u64) -> usize {
use std::sync::atomic::Ordering::Relaxed;
self.texture_slots_page_written(frame);
let ranges = std::mem::take(&mut *REMAPPED.lock().unwrap_or_else(|e| e.into_inner()));
let mut ranges = std::mem::take(&mut *REMAPPED.lock().unwrap_or_else(|e| e.into_inner()));
if ranges.is_empty() {
return 0;
}
join(&mut ranges);
let mut slots: Vec<u32> = {
let placed = self.placed.lock().unwrap_or_else(|e| e.into_inner());
ranges.iter().flat_map(|&(start, end)| placed.slots_in(start, end)).collect()
@@ -2022,24 +2055,36 @@ impl HostHeap {
/// `handle` views, or something else: until then it shows the neutral
/// texture ([`show_after`]).
pub(crate) fn texture_slot_show_after(&self, list: &crate::host::HostList, index: u32, handle: u64) {
if self.show_later(index, handle) {
self.flip(list, index, handle, Some(crate::frame::current()));
}
}
/// What slot `index` shows until the texture the record at `handle` views
/// is shown. False: it shows it, or there is none.
fn show_later(&self, index: u32, handle: u64) -> bool {
use std::sync::atomic::Ordering::Relaxed;
let key = record_key(handle);
let view = view_at(key as u32, (key >> 32) as u32);
let (true, Some(view)) = (key != 0, view) else {
return;
return false;
};
let stale = {
let _made = self.made.lock(index);
let Some(shown) = self.made.get(index) else {
return;
return false;
};
if shown == key {
return;
return false;
}
// (As `mirror`.)
let stale = self.note_buffer(index, Some(&view), true);
let old = view_at(shown as u32, (shown >> 32) as u32);
if shown == 0 || !old.is_some_and(|old| same_placed_texture(&old, &view)) {
let kept = old.is_some_and(|old| match kin(&old, &view) {
Kin::Same => true,
kin => kin.kept(keep_shown()),
});
if shown == 0 || !kept {
#[cfg(feature = "diagnostics")]
note_before(index, shown, "neutral");
self.quick_is(index, 0);
@@ -2053,7 +2098,21 @@ impl HostHeap {
stale
};
self.unmake_stale(index, stale);
self.flip(list, index, handle, Some(crate::frame::current()));
true
}
/// [`HostHeap::texture_slot_show_after`] of a texture whose upload `job`
/// makes: shown when that has run.
pub(crate) fn texture_slot_show_ahead(&self, job: &crate::texture::ahead::Job, index: u32, handle: u64) {
if !self.show_later(index, handle) {
return;
}
let (heap, key) = (std::ptr::from_ref(self) as usize, record_key(handle));
let late = Some(crate::frame::current());
job.then(move || {
let flip = Flip { after: 0, heap, index, handle, key, late };
FLIPS.lock().unwrap_or_else(|e| e.into_inner()).push(flip);
});
}
/// [`HostHeap::mirror`] of a slot whose table a draw read: a texture made
@@ -2067,6 +2126,7 @@ impl HostHeap {
&& let Some(View::Shader(resource, _)) = placed_texture(&view).or(Some(view))
&& crate::resource::buffer_bytes(resource as crate::object::This).is_none()
&& crate::resource::host_resource(resource as crate::object::This).is_some_and(crate::pool::fresh)
&& !crate::texture::ahead::there(resource as crate::object::This)
{
return self.texture_slot_show_after(list, index, handle);
}
@@ -2131,8 +2191,14 @@ impl HostHeap {
#[cfg(feature = "diagnostics")]
note_before(f.index, shown, if f.late.is_some() { "shown after its upload" } else { "flip" });
self.put(f.index);
#[cfg(feature = "diagnostics")]
let was = shown;
self.made.set(f.index, f.key);
self.keep(f.index, kept);
#[cfg(feature = "diagnostics")]
if f.late.is_some() && was != 0 {
same_bytes(was, f.key);
}
if let Some(since) = f.late {
let frames = crate::frame::current().saturating_sub(since);
let n = LATE[0].fetch_add(1, Relaxed) + 1;
@@ -2380,12 +2446,13 @@ impl HostHeap {
}
}
let stride = view.and_then(view_stride);
let kind = view.map_or(0, view_kind);
if let Some((_, _, data)) = &self.strides
&& index < victory_shader::emit::SLOT_STRIDES_COUNT
{
// SAFETY: inside the mapping; shaders read it while we write (a
// dword, either value is one the slot had).
unsafe { (*data as *mut u32).add(index as usize).write_volatile(stride.unwrap_or(0)) };
unsafe { (*data as *mut u32).add(index as usize).write_volatile(stride.unwrap_or(0) | kind) };
}
let mut buffers = self.buffers.lock().unwrap_or_else(|e| e.into_inner());
let was = if stride.is_some() {
@@ -2916,13 +2983,27 @@ impl HostHeap {
// time it got sharper.
if later
&& keep_views()
&& view_at(shown as u32, (shown >> 32) as u32)
.is_some_and(|old| same_placed_texture(&old, &view))
&& let Some(old) = view_at(shown as u32, (shown >> 32) as u32)
{
KEPT_VIEWS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
drop(held);
self.unmake_stale(index, stale);
return Some(view);
let kin = kin(&old, &view);
if kin == Kin::Same {
KEPT_VIEWS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
drop(held);
self.unmake_stale(index, stale);
return Some(view);
}
// And of another texture that can stand for it
// ([`keep_shown`]): black, then grey for a frame or two is
// what popped.
if is_texture_view(&old) {
let kept = kin.kept(keep_shown());
rewritten(kin, kept);
if kept {
drop(held);
self.unmake_stale(index, stale);
return Some(view);
}
}
}
#[cfg(feature = "diagnostics")]
if !self.sampler && is_texture_view(&view) {
@@ -4016,6 +4097,160 @@ fn keep_views() -> bool {
*ON.get_or_init(|| std::env::var_os("VICTORY_KEEP_VIEWS").is_none_or(|v| v != "0"))
}
/// What the texture view a slot is written with has to do with the one it
/// shows.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Kin {
/// A view of the same placed texture ([`same_placed_texture`]).
Same,
/// A texture of the same description placed elsewhere (moved, or one of
/// its kind).
Moved,
/// One of the same format and shape in another size: the same texture
/// with more or fewer levels, as a title streams it.
Sized,
/// A view of the same dimension, of another texture.
Other,
/// A view no shader that reads the one can read.
None,
}
impl Kin {
/// Whether the slot goes on showing what it shows, by [`keep_shown`].
fn kept(self, keep: u8) -> bool {
match self {
Kin::Same => true,
Kin::Moved | Kin::Sized => keep >= 1,
Kin::Other => keep >= 2,
Kin::None => false,
}
}
}
/// [`Kin`] of the view `old`, which a slot shows, and `new`.
fn kin(old: &View, new: &View) -> Kin {
if same_placed_texture(old, new) {
return Kin::Same;
}
if let (View::Placed(false, a, from), View::Placed(false, b, to)) = (old, new) {
if a[0] == 1 || b[0] == 1 || from[1] != to[1] {
return Kin::None;
}
// (Width, 64 bits, height, depth and levels, format: words 4 to 8.)
if a[0] == b[0] && a[2..13] == b[2..13] {
return Kin::Moved;
}
let (wide, high) = ([a[4], b[4]].map(u64::from), [a[6], b[6]].map(u64::from));
return if from[0] == to[0] && a[0] == b[0] && wide[0] * high[1] == wide[1] * high[0] {
Kin::Sized
} else {
Kin::Other
};
}
let dimension = |view: &View| match view {
View::Placed(false, resource, desc) if resource[0] != 1 => desc[1],
View::Shader(r, Some(desc)) if crate::resource::buffer_bytes(*r as crate::object::This).is_none() => desc[1],
_ => 0,
};
match (dimension(old), dimension(new)) {
(0, _) | (_, 0) => Kin::None,
(a, b) if a == b => Kin::Other,
_ => Kin::None,
}
}
/// Whether a slot that is written with a view of another texture goes on
/// showing the one it shows until the new one is made and its upload has run
/// (`VICTORY_KEEP_SHOWN`): 0: never, it reads as zeros, then as the neutral
/// texture; 1: one of the same kind in another place or size; 2: any of the
/// same dimension.
fn keep_shown() -> u8 {
#[cfg(feature = "diagnostics")]
if let Some(on) = crate::ab::pick("VICTORY_KEEP_SHOWN") {
return if on { 2 } else { 0 };
}
static KEEP: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
*KEEP.get_or_init(|| std::env::var("VICTORY_KEEP_SHOWN").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(1))
}
/// Slots that showed a texture and were written with a view of another,
/// since the start, by [`Kin`] (moved, sized, other, none), and those of
/// them that kept what they showed.
static REWRITTEN: [std::sync::atomic::AtomicU64; 5] = [const { std::sync::atomic::AtomicU64::new(0) }; 5];
fn rewritten(kin: Kin, kept: bool) {
use std::sync::atomic::Ordering::Relaxed;
let class = match kin {
Kin::Moved | Kin::Same => 0,
Kin::Sized => 1,
Kin::Other => 2,
Kin::None => 3,
};
REWRITTEN[class].fetch_add(1, Relaxed);
REWRITTEN[4].fetch_add(u64::from(kept), Relaxed);
let n: u64 = REWRITTEN[..4].iter().map(|n| n.load(Relaxed)).sum();
if n.is_multiple_of(5000) {
tracing::info!(
"slots that showed a texture and were written with another, since the start: {} of the same kind elsewhere, {} of another size, {} others, {} of another dimension; {} of them kept what they showed meanwhile (VICTORY_KEEP_SHOWN)",
REWRITTEN[0].load(Relaxed),
REWRITTEN[1].load(Relaxed),
REWRITTEN[2].load(Relaxed),
REWRITTEN[3].load(Relaxed),
REWRITTEN[4].load(Relaxed)
);
}
}
/// Diagnostics: whether a texture that a slot showed until its new one came
/// holds the new one's bytes (the title moved it), by the bytes their host
/// copies were filled from: equal, different, not known.
#[cfg(feature = "diagnostics")]
fn same_bytes(old: u64, new: u64) {
use std::sync::atomic::Ordering::Relaxed;
static SEEN: [std::sync::atomic::AtomicU64; 6] = [const { std::sync::atomic::AtomicU64::new(0) }; 6];
let view = |key: u64| view_at(key as u32, (key >> 32) as u32);
let (Some(old), Some(new)) = (view(old), view(new)) else {
return;
};
let held = |view: &View| {
let View::Placed(false, resource, desc) = view else {
return None;
};
let address = u64::from(desc[10]) | u64::from(desc[11]) << 32;
crate::resource::texture_held(crate::resource::texture_at_made(address, resource)?)
};
let class = match kin(&old, &new) {
Kin::Moved => 0,
Kin::Same | Kin::None => return,
_ => 3,
};
let which = match (held(&old), held(&new)) {
(Some(a), Some(b)) if a == b => 0,
(Some(a), Some(b)) if a.0 == b.0 => 1,
_ => 2,
};
SEEN[class + which].fetch_add(1, Relaxed);
let n: u64 = SEEN.iter().map(|n| n.load(Relaxed)).sum();
if n.is_multiple_of(1000) {
let seen: Vec<u64> = SEEN.iter().map(|n| n.load(Relaxed)).collect();
tracing::info!(
"slots that went on showing another texture until the new one came, since the start: of one of the same kind elsewhere {} had the new one's bytes, {} others, {} not known; of others {}, {}, {}",
seen[0], seen[1], seen[2], seen[3], seen[4], seen[5]
);
}
}
/// For `crate::popwatch`, since the start: slots made null as they were
/// written with another texture, such that kept the old one, slots given the
/// neutral texture, slots made null as their memory was another's.
#[cfg(feature = "diagnostics")]
pub(crate) fn pop_causes() -> [u64; 4] {
use std::sync::atomic::Ordering::Relaxed;
let all: u64 = REWRITTEN[..4].iter().map(|n| n.load(Relaxed)).sum();
let kept = REWRITTEN[4].load(Relaxed);
[all.saturating_sub(kept), kept, LATE[3].load(Relaxed), STALE[0].load(Relaxed)]
}
/// Whether a slot already made from its record is told so without the lock
/// (`VICTORY_QUICK_MIRROR=0`: with it, as before).
fn quick_mirror() -> bool {
@@ -4425,6 +4660,61 @@ mod tests {
assert!(!same_placed_texture(&low, &View::Shader(0, None)));
}
#[test]
fn ranges_that_touch_are_one() {
let mut ranges = vec![(40, 50), (30, 40), (35, 40), (10, 20), (45, 60), (60, 61), (1, 1)];
join(&mut ranges);
assert_eq!(ranges, [(1, 1), (10, 20), (30, 61)]);
join(&mut ranges);
assert_eq!(ranges.len(), 3);
}
/// Typed buffer views of integer formats are marked in the table of
/// strides; float, normalized, raw and structured ones are not.
#[test]
fn integer_typed_buffer_views_are_marked_in_the_table_of_strides() {
use victory_shader::emit::{VIEW_INTEGER, VIEW_SIGNED};
// Format, dimension (buffer), first element, count, stride, counter, flags.
let uav = |format, stride, flags| View::Unordered(0, 0, Some([format, 1, 0, 0, 64, stride, 0, 0, flags, 0]));
assert_eq!(view_kind(&uav(12, 0, 0)), VIEW_INTEGER, "R16G16B16A16_UINT");
assert_eq!(view_kind(&uav(59, 0, 0)), VIEW_INTEGER | VIEW_SIGNED, "R16_SINT");
assert_eq!(view_kind(&uav(10, 0, 0)), 0, "R16G16B16A16_FLOAT");
assert_eq!(view_kind(&uav(58, 0, 0)), 0, "R16_SNORM");
assert_eq!(view_kind(&uav(39, 0, 1)), 0, "raw");
assert_eq!(view_kind(&uav(0, 16, 0)), 0, "structured");
assert_eq!(view_stride(&uav(12, 0, 0)), Some(8));
}
/// What may stand for a texture that is not there yet: one of its kind in
/// another place or size; another one only if asked; nothing a shader
/// cannot read in its place.
#[test]
fn a_slot_shows_on_what_can_stand_for_its_new_texture() {
let texture = [3, 0, 0, 0, 512, 0, 256, 0x8_0001, 98, 1, 0, 0x105, 0, 0];
let view = |resource: [u32; 14], format: u32, dimension: u32, address: u32| {
let mut desc = [0u32; 16];
(desc[0], desc[1], desc[10], desc[11]) = (format, dimension, address, 0xb);
View::Placed(false, resource, desc)
};
let old = view(texture, 98, 4, 0x1000_0000);
let mut twice = texture;
(twice[4], twice[6], twice[7]) = (1024, 512, 0x9_0001);
let mut square = texture;
square[6] = 512;
let mut buffer = texture;
buffer[0] = 1;
assert_eq!(kin(&old, &view(texture, 98, 4, 0x1000_0000)), Kin::Same);
assert_eq!(kin(&old, &view(texture, 98, 4, 0x2000_0000)), Kin::Moved);
assert_eq!(kin(&old, &view(twice, 98, 4, 0x2000_0000)), Kin::Sized);
assert_eq!(kin(&old, &view(square, 98, 4, 0x2000_0000)), Kin::Other);
assert_eq!(kin(&old, &view(twice, 83, 4, 0x2000_0000)), Kin::Other);
assert_eq!(kin(&old, &view(texture, 98, 9, 0x2000_0000)), Kin::None);
assert_eq!(kin(&old, &view(buffer, 98, 1, 0x2000_0000)), Kin::None);
for (kin, kept) in [(Kin::Same, [true; 3]), (Kin::Moved, [false, true, true]), (Kin::Sized, [false, true, true]), (Kin::Other, [false, false, true]), (Kin::None, [false; 3])] {
assert_eq!([0, 1, 2].map(|keep| kin.kept(keep)), kept, "{kin:?}");
}
}
/// A constant buffer view larger than the host takes is cut to what it
/// takes: the device, which such a view removes, stays.
#[test]