CGBJ's renderer, DLSS, an F1 settings menu, frame generation, ray tracing switch

The default of scripts\play-nvidia.ps1 changes: the game renders its own
720p and DLSS takes it to 1080p (Series S), in place of the half render
scale and the title's upscaler. -Resolution still reaches the older path.

- Renderer: codec-d/cool_fox f4b48ad (GPL-3.0) with this fork's NVIDIA
  fixes on top (residency.rs, stretch.rs, the ffx upscale dispatch).
- DLSS for the title's upscale, from avatarmanz/VIctory-7900XTX pull
  request 203 (feature `dlss`, VICTORY_UPSCALE=dlss), with
  VICTORY_DLSS_PRESET and a live sharpness setting.
- F1 settings menu in the game (host/menu.rs): saved to
  victory-settings.txt, applied by the launcher at the next start,
  "save and restart" from the menu. F3: the panel, now with the
  generated frame rate beside the real one.
- DLSS frame generation through NVIDIA Streamline (streamline.rs, fg.rs),
  2x to 6x, the amount changed while playing. Constants and the depth and
  motion vector tags are handed over at each present.
- VICTORY_FPS_CAP: the title's own 30 frames a second can be lifted
  (60, 120, none), live from the menu.
- Real ray tracing as an option (-RayTracing, VICTORY_BVH_BUILDER=1): on
  this renderer the structure builder runs on NVIDIA. Frame generation is
  left off with it: the two together run at about 0.1 fps, cause not found.
- VICTORY_HEAP_RECHECK_EVERY: textures picked by index looked at again
  for the whole run (a test against the posterized look).
- Get-NVIDIA-DLSS.cmd / scripts\get-nvidia-sdks.ps1: fetch the few NVIDIA
  files DLSS and frame generation need (about 70 MB) from NVIDIA's GitHub
  into nvidia-sdk\ (git-ignored). They are NVIDIA's, under NVIDIA's
  licences, and are not part of this repository. Without them the picture
  is stretched and there is no frame generation.
- scripts\build-nvidia.ps1; the launcher builds the layer when it is not
  built, and again with DLSS once those files are there.

Measured on an RTX 5070 (Series S, DLSS, opening rooms): about 15-17 fps
without ray tracing, 13-15 with it, before generated frames. What is known
to be wrong is in the README's new section.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
trustno1-coderandClaude Fable 5.2 committed 2026-11-26 17:56:14 -08:00
1 parent 9e7ab9d9a2
commit 63957271cd
55 files changed
+8030 -1313

No files matched your search

+462 -57
View File
@@ -112,6 +112,8 @@ impl Made {
}
pub(crate) struct HostHeap {
pub(crate) buffer_sweep: Mutex<crate::draw::BufferSweep>,
pub(crate) buffer_frame: std::sync::atomic::AtomicU64,
pub(crate) heap: ID3D12DescriptorHeap,
cpu: usize,
step: u32,
@@ -424,6 +426,48 @@ fn neutral_for(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) -> Option<ID3D12Res
static FLIPS: Mutex<Vec<Flip>> = Mutex::new(Vec::new());
/// Deferred slot flips recorded into an open or not-yet-submitted command
/// list (`HostHeap::flip`), tracked so a one-shot decoration bake draw can
/// promote them out of the `neutral()` gray stand-in before sampling them.
pub(crate) struct PendingListFlip {
id: u64,
pub(crate) origin: (usize, u64),
flip: Flip,
}
impl PendingListFlip {
pub(crate) fn index(&self) -> u32 {
self.flip.index
}
pub(crate) fn handle(&self) -> u64 {
self.flip.handle
}
pub(crate) fn key(&self) -> u64 {
self.flip.key
}
}
static NEXT_PENDING_FLIP: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
static PENDING_LIST_FLIPS: Mutex<Vec<PendingListFlip>> = Mutex::new(Vec::new());
static PENDING_LIST_COUNT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
fn take_pending_list_flip(id: u64) -> Option<Flip> {
if PENDING_LIST_COUNT.load(std::sync::atomic::Ordering::Acquire) == 0 {
return None;
}
let mut pending = PENDING_LIST_FLIPS.lock().unwrap_or_else(|e| e.into_inner());
let pos = pending.iter().position(|p| p.id == id)?;
let entry = pending.swap_remove(pos);
PENDING_LIST_COUNT.store(pending.len(), std::sync::atomic::Ordering::Release);
Some(entry.flip)
}
fn remove_pending_list_flip(id: u64) {
let _ = take_pending_list_flip(id);
}
/// 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.
@@ -712,6 +756,12 @@ impl Spans {
/// pass last took the ranges ([`page_written`]). Kept apart from
/// [`REMAPPED`] so that what such writes do is counted by itself.
static PAGE_WRITTEN: Mutex<Vec<(u64, u64)>> = Mutex::new(Vec::new());
/// Physical-page writes retained specifically for one-shot decoration/ped
/// texture bakes when `VICTORY_PAGE_WRITES` bit 2 is off (the default): normal
/// frames stay free of queue flooding while every bake still sees freshly
/// DirectStorage-streamed texture bytes immediately instead of waiting on
/// `RECHECK_FRAMES`.
static BAKE_PAGE_WRITTEN: Mutex<Vec<(u64, u64)>> = Mutex::new(Vec::new());
/// Since the start: such ranges kept for a pass; ranges dropped because no
/// pass took them; slots of placed textures that held bytes of them when a
@@ -1134,29 +1184,46 @@ pub(crate) fn device_removed() -> Option<String> {
pub(crate) static PLACED_TEXTURE_SLOTS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
pub(crate) static OBJECT_TEXTURE_SLOTS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// Bytes per element of a buffer view, as the console's descriptor holds
/// them for indexed reads (structured: the stride; raw: 1, the index is the
/// byte; typed: the format's); `None` if `view` is not a buffer view.
fn view_stride(view: &View) -> Option<u32> {
let (stride, raw, format) = view_elements(view)?;
Some(if stride > 0 {
/// Bytes per element and typed integer format bits of a buffer view, decoded
/// in a single pass over the view descriptor; `(None, 0)` if not a buffer view.
fn view_stride_and_kind(view: &View) -> (Option<u32>, u32) {
use victory_shader::emit::{VIEW_INTEGER, VIEW_SIGNED};
let Some((stride, raw, format)) = view_elements(view) else {
return (None, 0);
};
let bytes = if stride > 0 {
stride
} else if raw {
1
} else {
crate::resource::element(format).map_or(0, |(bytes, _)| bytes as u32)
})
};
let kind = if stride == 0 && !raw {
match format {
3 | 7 | 12 | 17 | 25 | 30 | 36 | 42 | 50 | 57 | 62 => VIEW_INTEGER,
4 | 8 | 14 | 18 | 32 | 38 | 43 | 52 | 59 | 64 => VIEW_INTEGER | VIEW_SIGNED,
_ => 0,
}
} else {
0
};
(Some(bytes), kind)
}
/// 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.
/// Packed [`victory_shader::emit::SLOT_STRIDES`] word for `view`.
fn view_stride_word(view: &View) -> u32 {
let (stride, kind) = view_stride_and_kind(view);
stride.unwrap_or(0) | kind
}
#[cfg(test)]
fn view_stride(view: &View) -> Option<u32> {
view_stride_and_kind(view).0
}
#[cfg(test)]
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,
}
view_stride_and_kind(view).1
}
/// A buffer view's structure stride, whether it is raw, and its format;
@@ -1568,6 +1635,8 @@ impl HostHeap {
})
.flatten();
let h = HostHeap {
buffer_sweep: Mutex::default(),
buffer_frame: std::sync::atomic::AtomicU64::new(u64::MAX),
heap,
cpu,
step,
@@ -1675,10 +1744,16 @@ impl HostHeap {
}
/// A plain point sampler at `at`.
/// LOCAL (11/26, `VICTORY_NULL_SAMPLER_LINEAR=1`): a linear one instead. The
/// oil-painting look of some runs is posterization (colour bands), as from a
/// colour-grading LUT read with a point sampler: a sampler slot whose
/// description was missed at start would read as this one all run.
fn null_sampler(at: D3D12_CPU_DESCRIPTOR_HANDLE) {
let Some(host) = host() else { return };
static LINEAR: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
let linear = *LINEAR.get_or_init(|| std::env::var_os("VICTORY_NULL_SAMPLER_LINEAR").is_some_and(|v| v == "1"));
let desc = D3D12_SAMPLER_DESC {
Filter: D3D12_FILTER_MIN_MAG_MIP_POINT,
Filter: if linear { D3D12_FILTER_MIN_MAG_MIP_LINEAR } else { D3D12_FILTER_MIN_MAG_MIP_POINT },
AddressU: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
AddressV: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
AddressW: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
@@ -1780,7 +1855,7 @@ impl HostHeap {
pub(crate) fn texture_news(&self) -> (u64, bool) {
let writes = self.textures.lock().unwrap_or_else(|e| e.into_inner()).writes;
let noted = |ranges: &Mutex<Vec<(u64, u64)>>| !ranges.lock().unwrap_or_else(|e| e.into_inner()).is_empty();
(writes, noted(&REMAPPED) || noted(&PAGE_WRITTEN))
(writes, noted(&REMAPPED) || noted(&PAGE_WRITTEN) || noted(&BAKE_PAGE_WRITTEN))
}
/// Every title slot of the queue that is due at `frame`, and whether
@@ -1928,7 +2003,48 @@ impl HostHeap {
if ranges.is_empty() {
return 0;
}
self.slots_written_under(&ranges, frame, crate::watch::page_writes() & 2 != 0)[2]
let due = crate::watch::page_writes() & 2 != 0;
let made_due = self.slots_written_under(&ranges, frame, due)[2];
if !due {
let mut kept = BAKE_PAGE_WRITTEN.lock().unwrap_or_else(|e| e.into_inner());
kept.extend(ranges);
if kept.len() > 256 {
join(&mut kept);
const MOST: usize = 1 << 14;
if kept.len() > MOST {
let drop_n = kept.len() - MOST;
kept.drain(..drop_n);
}
}
}
made_due
}
/// Wakes uploaded placed-texture slots touched by recent physical-page
/// writes (DirectStorage completions) immediately (`due = frame`) right
/// before a one-shot ped/decoration bake draw.
pub(crate) fn texture_slots_bake_written(&self, frame: u64) -> usize {
let mut ranges = std::mem::take(&mut *BAKE_PAGE_WRITTEN.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_holding(start, end)).collect()
};
if slots.is_empty() {
return 0;
}
slots.sort_unstable();
slots.dedup();
slots.retain(|&slot| !self.tabled_lately(slot, frame));
let mut textures = self.textures.lock().unwrap_or_else(|e| e.into_inner());
slots.retain(|slot| textures.slots.get(slot).is_none_or(|w| w.mapped > 0 || w.checked.2 != 0));
for &slot in &slots {
textures.changed(slot, frame, frame);
}
slots.len()
}
/// [`HostHeap::texture_slots_page_written`] for `ranges` (start, end):
@@ -2223,6 +2339,7 @@ impl HostHeap {
/// reads the table's dynamic slots).
pub(crate) fn mirror_after(&self, list: &crate::host::HostList, index: u32, handle: u64) {
if show_after()
&& !crate::texture::BAKING.get()
&& index < self.count
&& !self.texture_slot_made_from(index, handle)
&& let Some(view) = super::view(handle)
@@ -2237,14 +2354,143 @@ impl HostHeap {
}
fn flip(&self, list: &crate::host::HostList, index: u32, handle: u64, late: Option<u64>) {
use std::sync::atomic::Ordering::{Relaxed, Release};
let (heap, key) = (std::ptr::from_ref(self) as usize, record_key(handle));
let id = NEXT_PENDING_FLIP.fetch_add(1, Relaxed);
let origin = (std::ptr::from_ref(list) as usize, list.epoch());
{
let mut pending = PENDING_LIST_FLIPS.lock().unwrap_or_else(|e| e.into_inner());
pending.push(PendingListFlip {
id,
origin,
flip: Flip { after: 0, heap, index, handle, key, late },
});
PENDING_LIST_COUNT.store(pending.len(), Release);
}
struct PendingGuard(u64);
impl PendingGuard {
fn id(&self) -> u64 {
self.0
}
}
impl Drop for PendingGuard {
fn drop(&mut self) {
remove_pending_list_flip(self.0);
}
}
let guard = PendingGuard(id);
list.defer_host(move || {
let after = crate::host::submissions().0;
let flip = Flip { after, heap, index, handle, key, late };
// Call a `&self` method so Rust 2021+ moves the whole `guard`
// into this closure rather than copying `guard.0` and dropping
// `guard` immediately at the end of `flip()`.
let Some(mut flip) = take_pending_list_flip(guard.id()) else {
return;
};
flip.after = crate::host::submissions().0;
FLIPS.lock().unwrap_or_else(|e| e.into_inner()).push(flip);
});
}
/// Whether any deferred slot flip or view remake for this heap requires
/// settling in-flight GPU submissions before exposing real textures to a
/// one-shot decoration bake draw.
pub(crate) fn needs_gpu_settle_for_bake(&self) -> bool {
let own = std::ptr::from_ref(self) as usize;
let finished = crate::host::submissions().1;
if PENDING_LIST_COUNT.load(std::sync::atomic::Ordering::Acquire) > 0 {
let pending = PENDING_LIST_FLIPS.lock().unwrap_or_else(|e| e.into_inner());
if pending.iter().any(|p| p.flip.heap == own) {
return true;
}
}
{
let flips = FLIPS.lock().unwrap_or_else(|e| e.into_inner());
if flips.iter().any(|f| f.heap == own && f.after > finished) {
return true;
}
}
let after = RESHOWN_AFTER.lock().unwrap_or_else(|e| e.into_inner());
after.iter().any(|&(at, _)| at > finished)
}
/// Drains deferred flips still sitting in unsubmitted command lists for
/// this heap so a decoration bake can mirror them out of `neutral()`.
/// Entries that cannot be promoted yet (unmapped memory or deferred
/// staging) must be returned via [`HostHeap::restore_pending_list_flip`].
pub(crate) fn take_pending_list_flips(&self) -> Vec<PendingListFlip> {
if PENDING_LIST_COUNT.load(std::sync::atomic::Ordering::Acquire) == 0 {
return Vec::new();
}
let own = std::ptr::from_ref(self) as usize;
let mut pending = PENDING_LIST_FLIPS.lock().unwrap_or_else(|e| e.into_inner());
let (mine, rest): (Vec<PendingListFlip>, Vec<PendingListFlip>) =
pending.drain(..).partition(|p| p.flip.heap == own);
*pending = rest;
PENDING_LIST_COUNT.store(pending.len(), std::sync::atomic::Ordering::Release);
mine
}
/// Re-queues an unpromoted deferred flip drained by
/// [`HostHeap::take_pending_list_flips`] so its originating command list
/// still flips it out of `neutral()` once submitted.
pub(crate) fn restore_pending_list_flip(&self, item: PendingListFlip) {
let mut pending = PENDING_LIST_FLIPS.lock().unwrap_or_else(|e| e.into_inner());
pending.push(item);
PENDING_LIST_COUNT.store(pending.len(), std::sync::atomic::Ordering::Release);
}
/// Whether title descriptor slot `handle` still holds record `key`.
pub(crate) fn slot_still_at(&self, handle: u64, key: u64) -> bool {
key != 0 && record_key(handle) == key
}
/// Looks up `(view, key)` for slot `index` prior to a synchronous bake
/// upload without replacing its current host descriptor with `neutral()`;
/// [`HostHeap::publish_uploaded_slot`] exposes the real descriptor and
/// updates `Spans`/stride tracking once the GPU copy finishes.
pub(crate) fn prepare_bake_slot(&self, index: u32, handle: u64) -> Option<(View, u64)> {
if index >= self.count {
return None;
}
let key = record_key(handle);
let view = (key != 0).then(|| view_at(key as u32, (key >> 32) as u32)).flatten()?;
Some((view, key))
}
/// Exposes the real host descriptor for slot `index` after its synchronous
/// bake upload has completed on the GPU, updating `Spans`/stride tracking
/// without re-queuing the slot in `TextureQueue`.
pub(crate) fn publish_uploaded_slot(&self, index: u32, handle: u64, key: u64) -> bool {
if index >= self.count || key == 0 {
return false;
}
let (published, stale) = {
let _made = self.made.lock(index);
if self.made.get(index) == Some(key) {
return false;
}
if record_key(handle) != key {
return false;
}
let Some(view) = view_at(key as u32, (key >> 32) as u32) else {
return false;
};
let stale = self.note_buffer(index, Some(&view), true);
if let Some(kept) = make(&view, self.at(index)) {
self.put(index);
self.quick_is(index, 0);
self.clear_twin(index);
self.made.set(index, key);
self.keep(index, kept);
(true, stale)
} else {
(false, stale)
}
};
self.unmake_stale(index, stale);
published
}
/// Makes the slots whose new view's upload has run
/// ([`HostHeap::texture_slot_flip`]); returns how many.
pub(crate) fn texture_slots_flip(&self) -> usize {
@@ -2262,6 +2508,7 @@ impl HostHeap {
ready
};
let mut flipped = 0;
let mut late_flipped = 0;
for f in ready {
let _made = self.made.lock(f.index);
let Some(shown) = self.made.get(f.index) else {
@@ -2304,6 +2551,7 @@ impl HostHeap {
same_bytes(was, f.key);
}
if let Some(since) = f.late {
late_flipped += 1;
let frames = crate::frame::current().saturating_sub(since);
let n = LATE[0].fetch_add(1, Relaxed) + 1;
let all = LATE[1].fetch_add(frames, Relaxed) + frames;
@@ -2322,7 +2570,7 @@ impl HostHeap {
}
}
FLIPPED[0].fetch_add(flipped as u64, Relaxed);
flipped
flipped + late_flipped
}
/// The memory of the texture title slot `index` views is there (again):
@@ -2351,6 +2599,7 @@ impl HostHeap {
}
self.put(index);
self.clear_twin(index);
self.write_stride(index, 0);
self.keep(index, None);
}
@@ -2546,24 +2795,17 @@ impl HostHeap {
textures.remove(slot);
}
}
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
{
// LOCAL (port of fxceless #1216): constant buffer views marked;
// programs read no other non-buffer slot as one.
let entry = match (stride, view) {
(Some(s), _) => s | kind,
(None, Some(View::Constant(..))) if victory_shader::emit::constant_marks() => {
victory_shader::emit::SLOT_STRIDE_CONSTANT
}
_ => kind,
};
// 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(entry) };
}
let (stride, kind) = view.map_or((None, 0), view_stride_and_kind);
// LOCAL (port of fxceless #1216): constant buffer views marked;
// programs read no other non-buffer slot as one.
let entry = match (stride, view) {
(Some(s), _) => s | kind,
(None, Some(View::Constant(..))) if victory_shader::emit::constant_marks() => {
victory_shader::emit::SLOT_STRIDE_CONSTANT
}
_ => kind,
};
self.write_stride(index, entry);
let was = if stride.is_some() {
self.buffers.put(index);
true
@@ -2576,6 +2818,20 @@ impl HostHeap {
stale
}
/// Writes `word` (stride | format kind) for view-heap slot `index` into
/// the host's [`victory_shader::emit::SLOT_STRIDES`] table.
#[inline]
fn write_stride(&self, index: u32, word: u32) {
if let Some((_, _, data)) = &self.strides
&& index < victory_shader::emit::SLOT_STRIDES_COUNT
{
// SAFETY: inside the mapping; shaders read it while we write (a
// single aligned dword: either value is one the slot had, and
// dynamic slots are exclusively owned until submission finishes).
unsafe { (*data as *mut u32).add(index as usize).write_volatile(word) };
}
}
/// Gives back `n` dynamic slots from `first` (an index [`dynamic`]
/// returned).
///
@@ -2603,6 +2859,7 @@ impl HostHeap {
}
self.put(index);
self.twin_made(index, kept.is_some() && twinned(view));
self.write_stride(index, if kept.is_some() { view_stride_word(view) } else { 0 });
self.keep(index, kept.flatten());
}
@@ -2635,13 +2892,14 @@ impl HostHeap {
}
let twin = twinned(&view);
let kept = make_twinned(&view, views.slot(at), Some(views.slot(at + 1)))?;
let stride_word = view_stride_word(&view);
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 });
views.made.insert(words, ListView { view, at, twin, stride_word, kept, listed });
}
let made = &views.made[&words];
self.copy_from(index, views.slot(made.at));
@@ -2651,6 +2909,7 @@ impl HostHeap {
} else {
self.clear_twin(index);
}
self.write_stride(index, made.stride_word);
self.keep(index, made.kept.clone());
Some(made.listed)
})
@@ -2675,6 +2934,7 @@ impl HostHeap {
} else {
null_view(self.at(index));
self.put(index);
self.write_stride(index, 0);
}
self.clear_twin(index);
self.keep(index, None);
@@ -2761,6 +3021,7 @@ impl HostHeap {
self.put(index);
// (Bound by its handle, not read by index.)
self.clear_twin(index);
self.write_stride(index, 1);
self.keep(index, Some(buffer.clone()));
}
@@ -2770,6 +3031,7 @@ impl HostHeap {
null_buffer_view(self.at(index));
self.put(index);
self.clear_twin(index);
self.write_stride(index, 0);
self.keep(index, None);
}
@@ -2804,6 +3066,7 @@ impl HostHeap {
};
self.put(index);
self.clear_twin(index);
self.write_stride(index, 1);
self.keep(index, Some(buffer.clone()));
}
@@ -2833,6 +3096,7 @@ impl HostHeap {
};
self.put(index);
self.clear_twin(index);
self.write_stride(index, 0);
self.keep(index, None);
}
@@ -2890,6 +3154,7 @@ impl HostHeap {
unsafe { host.device.CreateShaderResourceView(buffer, Some(&raw), twin) };
}
self.twin_made(index, twin.is_some());
self.write_stride(index, 0);
// After the descriptor: what it showed is let go when nothing shows
// it any more.
self.keep(index, holds.then(|| buffer.clone()));
@@ -4203,24 +4468,27 @@ fn make_twinned(
}
const BUFFER: u32 = 1;
if resource[0] != BUFFER {
// shortcut: unordered access to a texture the title placed
// itself is not mirrored; give it a resource as shader
// views of such textures get when a dispatch writes one.
// Each texture is said once (the first 64), with how many times
// such a view was asked for so far: what shaders write through
// them is lost.
static SAID: Mutex<std::collections::BTreeSet<u64>> = Mutex::new(std::collections::BTreeSet::new());
static TIMES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let times = TIMES.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
let at = u64::from(desc[10]) | u64::from(desc[11]) << 32;
let mut said = SAID.lock().unwrap_or_else(|e| e.into_inner());
if (said.len() < 64 && said.insert(at)) || times.is_power_of_two() {
tracing::warn!(
address = format_args!("{at:#x}"),
times,
"placed unordered texture view not mirrored (null: writes through it are lost): resource {resource:?}, view {:?}",
&desc[..10]
);
if *unordered || at != 0 {
// shortcut: unordered access to a texture the title placed
// itself is not mirrored; give it a resource as shader
// views of such textures get when a dispatch writes one.
// Each texture is said once (the first 64), with how many times
// such a view was asked for so far: what shaders write through
// them is lost.
static SAID: Mutex<std::collections::BTreeSet<u64>> = Mutex::new(std::collections::BTreeSet::new());
static TIMES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let times = TIMES.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
let mut said = SAID.lock().unwrap_or_else(|e| e.into_inner());
if (said.len() < 64 && said.insert(at)) || times.is_power_of_two() {
tracing::warn!(
address = format_args!("{at:#x}"),
unordered = *unordered,
times,
"placed texture view not mirrored (null): resource {resource:?}, view {:?}",
&desc[..10]
);
}
}
return None;
}
@@ -5827,6 +6095,71 @@ void BY_FLOATS_OR_NOT(uint3 id : SV_DispatchThreadID) {
heap.release(last, 1);
assert_eq!(heap.dynamic(2), None);
}
#[test]
fn dynamic_slots_populate_and_clear_stride_table() {
use victory_shader::emit::{VIEW_INTEGER, VIEW_SIGNED};
let Some(heap) = HostHeap::new(false, 4) else {
return; // no host GPU
};
let Some((_, _, data)) = heap.strides else {
panic!("view heap should allocate strides table");
};
let read_stride = |slot: u32| -> u32 {
// SAFETY: inside `SLOT_STRIDES_COUNT` mapping of `heap.strides`.
unsafe { *(data as *const u32).add(slot as usize) }
};
let memory = vec![0u64; 1 << 11];
let addr = (memory.as_ptr() as u64).next_multiple_of(4096);
let placed_typed = |uav: bool, dxgi_fmt: u32, elems: u32| {
let resource: [u32; 14] = [1, 0, 0, 0, 4096, 0, 1, 1 | 1 << 16, 0, 1, 0, 1, 0, 0];
let mut desc = [0u32; 16];
// [format, dim=1 (buffer), mapping/pad, ..., num_elements @6, stride @7, flags @8]
let words = if uav {
[dxgi_fmt, 1, 0, 0, elems, 0, 0, 0, 0, 0]
} else {
[dxgi_fmt, 1, 5768, 0, 0, 0, elems, 0, 0, 0]
};
desc[..10].copy_from_slice(&words);
(desc[10], desc[11]) = (addr as u32, (addr >> 32) as u32);
View::Placed(uav, resource, desc)
};
let s0 = heap.dynamic(4).unwrap();
// R16G16B16A16_UINT (12): 8 bytes + VIEW_INTEGER
let u16x4 = placed_typed(true, 12, 64);
heap.make_at(s0, &u16x4);
assert_eq!(read_stride(s0), 8 | VIEW_INTEGER);
// R16_SINT (59): 2 bytes + VIEW_INTEGER | VIEW_SIGNED
let s16 = placed_typed(false, 59, 64);
heap.make_at(s0 + 1, &s16);
assert_eq!(read_stride(s0 + 1), 2 | VIEW_INTEGER | VIEW_SIGNED);
// R16G16B16A16_FLOAT (10): 8 bytes, no integer bits
let f16x4 = placed_typed(true, 10, 64);
heap.make_at(s0 + 2, &f16x4);
assert_eq!(read_stride(s0 + 2), 8);
// Clearing / nulling / raw views overwrite stale bits cleanly.
heap.make_null(s0);
assert_eq!(read_stride(s0), 0);
heap.null_raw_at(s0 + 1);
assert_eq!(read_stride(s0 + 1), 0);
heap.null_raw_uav_at(s0 + 2);
assert_eq!(read_stride(s0 + 2), 0);
// `make_listed` (both cold creation and cached copy hit) must populate strides.
let mut slot_mem = [0u32; 8];
crate::descriptor::record(slot_mem.as_mut_ptr() as u64, u16x4);
let rec = [slot_mem[0], slot_mem[1], slot_mem[2]];
assert!(heap.make_listed(0x1234, rec, s0).is_some());
assert_eq!(read_stride(s0), 8 | VIEW_INTEGER);
heap.make_null(s0 + 3);
assert_eq!(read_stride(s0 + 3), 0);
assert!(heap.make_listed(0x1234, rec, s0 + 3).is_some());
assert_eq!(read_stride(s0 + 3), 8 | VIEW_INTEGER);
}
}
/// What is known of a record a list's tables hold, in that recording of the
@@ -5847,6 +6180,7 @@ struct ListView {
/// Its descriptor's slot in [`ListViews`]; its raw twin's is the next.
at: u32,
twin: bool,
stride_word: u32,
kept: Kept,
listed: Listed,
}
@@ -5925,3 +6259,74 @@ pub(crate) fn note_listed(recording: u64, words: [u32; 3], listed: Listed) {
pub(crate) fn list_views() -> bool {
crate::toggled!("VICTORY_LIST_VIEWS")
}
#[cfg(test)]
mod bake_flip_tests {
use super::*;
#[test]
fn pending_list_flips_promote_once_and_clean_up() {
let heap = 0xdead_beefusize;
let id1 = NEXT_PENDING_FLIP.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let id2 = NEXT_PENDING_FLIP.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
{
let mut pending = PENDING_LIST_FLIPS.lock().unwrap_or_else(|e| e.into_inner());
pending.push(PendingListFlip {
id: id1,
origin: (1, 1),
flip: Flip { after: 0, heap, index: 7, handle: 0, key: 11, late: Some(1) },
});
pending.push(PendingListFlip {
id: id2,
origin: (2, 1),
flip: Flip { after: 0, heap, index: 8, handle: 0, key: 12, late: Some(1) },
});
PENDING_LIST_COUNT.store(pending.len(), std::sync::atomic::Ordering::Release);
}
let taken = take_pending_list_flip(id1).expect("id1 should be present");
assert_eq!(taken.index, 7);
assert!(take_pending_list_flip(id1).is_none(), "second take must be a no-op");
remove_pending_list_flip(id2);
assert!(take_pending_list_flip(id2).is_none(), "removed entry must be gone");
}
#[test]
fn deferred_flip_closure_keeps_pending_entry_alive_until_called_or_dropped() {
let heap = 0xcafe_babeusize;
let id1 = NEXT_PENDING_FLIP.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let id2 = NEXT_PENDING_FLIP.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
{
let mut pending = PENDING_LIST_FLIPS.lock().unwrap_or_else(|e| e.into_inner());
for (id, idx) in [(id1, 10), (id2, 11)] {
pending.push(PendingListFlip {
id,
origin: (3, 1),
flip: Flip { after: 0, heap, index: idx, handle: 0, key: 20, late: Some(1) },
});
}
PENDING_LIST_COUNT.store(pending.len(), std::sync::atomic::Ordering::Release);
}
struct TestGuard(u64);
impl TestGuard {
fn id(&self) -> u64 {
self.0
}
}
impl Drop for TestGuard {
fn drop(&mut self) {
remove_pending_list_flip(self.0);
}
}
let make_cb = |id: u64| -> Box<dyn Fn() -> Option<u32> + Send + 'static> {
let guard = TestGuard(id);
Box::new(move || take_pending_list_flip(guard.id()).map(|f| f.index))
};
let cb1 = make_cb(id1);
let cb2 = make_cb(id2);
// Must still be present after make_cb returns (guard moved, not dropped).
assert_eq!(cb1(), Some(10));
assert_eq!(cb1(), None);
drop(cb2);
assert!(take_pending_list_flip(id2).is_none(), "dropping uncalled closure cleans up entry");
}
}