Merge agent/textures: null bake sources left out, texture room line from the budget

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
readmet3xt-labandClaude Opus 5.5 committed 2026-11-26 17:12:21 +05:30
commit 33687f9fe0
4 files changed
+151 -35

No files matched your search

+55 -21
View File
@@ -250,6 +250,16 @@ fn bake_candidates(words: Vec<u32>, names_texture: impl Fn(u32) -> bool) -> Vec<
.collect()
}
/// Says why a bake's source could not be brought up to date (the first
/// 64 times): the bake reads it as it stands.
fn bake_source_failed(index: u32, view: &View, why: &str) {
static SAID: AtomicU64 = AtomicU64::new(0);
if SAID.fetch_add(1, Ordering::Relaxed) < 64 {
let text: String = format!("{view:?}").chars().take(200).collect();
tracing::warn!(slot = index, frame = crate::frame::current(), "a decoration bake's source was not brought up to date: {why}; {text}");
}
}
/// Adds one bake's counts to [`BAKE_SOURCES`] and says them: every bake
/// that found a source not up to date (the first 256), and the totals at
/// bakes 1, 2, 4, 8 ...
@@ -886,11 +896,14 @@ pub(crate) static HEAP_TEXTURES_PARTLY: [AtomicU64; 3] = [const { AtomicU64::new
/// last 5-second line, on: looks again at slots uploaded whole (a
/// fingerprint of 48 KiB), asking how much of a texture's memory is mapped,
/// asking whether a host copy holds what memory holds (a digest of all of
/// it), and making host copies and staging their uploads (`readable`); and
/// the passes in all. The pass stops at `VICTORY_TEXTURE_STAGE_MS` (16 in
/// it), staging uploads (`readable`); the passes in all; making host copies
/// (`resource::host_resource`: creation, residency); and making the slots'
/// views (`mirror`, the placed texture's resource). In the first runs with
/// the line (2026-11-26 16:52) the first four were a third of the passes'
/// time. The pass stops at `VICTORY_TEXTURE_STAGE_MS` (16 in
/// the launcher), and in play it ran 18-20 ms a frame for about 20
/// textures: these say what took the time.
pub(crate) static HEAP_TEXTURE_MICROS: [AtomicU64; 5] = [const { AtomicU64::new(0) }; 5];
pub(crate) static HEAP_TEXTURE_MICROS: [AtomicU64; 7] = [const { AtomicU64::new(0) }; 7];
/// `f`'s result, its time added to [`HEAP_TEXTURE_MICROS`]' place `slot`.
fn timed<T>(slot: usize, f: impl FnOnce() -> T) -> T {
@@ -2441,11 +2454,13 @@ impl State {
}
}
}
// (Not a null view: slot 1 and others hold views of no resource,
// which small numbers among the words name all the time; they were
// the "failed" sources of the first runs, 588 of 3,720.)
bake_candidates(words, |w| {
heap.handle(w)
.and_then(crate::descriptor::view)
.as_ref()
.is_some_and(crate::descriptor::host::is_texture_view)
heap.handle(w).and_then(crate::descriptor::view).as_ref().is_some_and(|view| {
crate::descriptor::host::is_texture_view(view) && !matches!(view, View::Shader(0, _))
})
})
}
@@ -2505,6 +2520,7 @@ impl State {
let Some(texture) = crate::resource::host_resource(resource) else {
// The host refused it (its creation failed).
n[8] += 1;
bake_source_failed(index, &view, "the host has no copy of it (refused)");
continue;
};
if !crate::texture::title_written(texture) || crate::texture::host_wrote(resource as usize) {
@@ -2526,15 +2542,31 @@ impl State {
// another view; the texture has no host copy yet; its host copy
// holds other bytes than its memory now.
n[if !shown { 3 } else if !had_copy { 4 } else { 7 }] += 1;
// The pass recorded its upload on this list already (with
// `BAKING`: read exactly at submission): nothing more to do.
if crate::texture::upload_listed(list, resource, first) {
sync.publish.push((index, handle, key));
continue;
}
let was_staged = crate::texture::is_staged(resource);
let _ = crate::residency::take_staging_deferred();
crate::texture::UPLOAD_STAGED.set(false);
readable(list, ResourceType::Srv, &view);
let failed = crate::residency::take_staging_deferred() || crate::residency::upload_refused();
let why = if crate::residency::take_staging_deferred() {
Some("its staging buffer was deferred")
} else if crate::residency::upload_refused() {
Some("uploads are refused for a moment after the host ran out of memory")
} else if !crate::texture::UPLOAD_STAGED.get() {
Some("no upload was recorded (no console layout, an XOR mode, or no staging buffer)")
} else {
None
};
if !was_staged {
crate::texture::unstage(resource);
}
if failed {
if let Some(why) = why {
n[8] += 1;
bake_source_failed(index, &view, why);
continue;
}
if let Some(copy) = crate::resource::host_copy(resource) {
@@ -3104,23 +3136,25 @@ impl State {
&& !sync_bake
&& crate::descriptor::host::show_after()
&& heap.handle(index).is_some_and(|handle| !host.texture_slot_made_from(index, handle));
let (view, bake_key) = if sync_bake {
match heap.handle(index).and_then(|handle| host.prepare_bake_slot(index, handle)) {
Some((view, key)) => (Some(view), Some(key)),
None => (None, None),
let (view, bake_key) = timed(6, || {
if sync_bake {
match heap.handle(index).and_then(|handle| host.prepare_bake_slot(index, handle)) {
Some((view, key)) => (Some(view), Some(key)),
None => (None, None),
}
} else {
let view = heap.handle(index).and_then(|handle| {
if shows_other || late { crate::descriptor::view(handle) } else { host.mirror(index, handle, true) }
});
(view, None)
}
} else {
let view = heap.handle(index).and_then(|handle| {
if shows_other || late { crate::descriptor::view(handle) } else { host.mirror(index, handle, true) }
});
(view, None)
};
});
let at_once = || {
if (late || bake_key.is_some()) && let Some(handle) = heap.handle(index) {
let _ = host.mirror(index, handle, true);
}
};
let placed = view.as_ref().and_then(crate::descriptor::host::placed_texture);
let placed = timed(6, || view.as_ref().and_then(crate::descriptor::host::placed_texture));
if crate::residency::upload_refused() {
served = false;
break;
@@ -3133,7 +3167,7 @@ impl State {
continue;
}
};
let texture = crate::resource::host_resource(resource);
let texture = timed(5, || crate::resource::host_resource(resource));
if crate::residency::upload_refused() {
served = false;
break;
+31 -10
View File
@@ -149,20 +149,31 @@ pub(crate) fn available_physical_ram() -> Option<u64> {
/// (By those times everything idle for 2.5 s went from 75 % of the budget
/// on: 61,000 of them in the first 300 s of session play-1125-0203, at 78 to
/// 80 %, of which 13,000 were made and uploaded again.)
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn vram_keep(budget: u64, used: u64) -> (u64, u64) {
vram_keep_below(budget, used, budget / 100 * 90)
}
/// [`vram_keep`] around the texture queue's line `line` (`crate::resource::room_line`)
/// instead of 90 % of the budget: none go while `used` is 2 % of the budget
/// under it or more, those idle 5 s go down to 5 % under it while it is
/// under 5 % over it, those idle 1.5 s above that. (At a line of 90 %: 88,
/// 85 and 95 %, as [`vram_keep`] was.)
pub(crate) fn vram_keep_below(budget: u64, used: u64, line: u64) -> (u64, u64) {
const KEPT_MS: u64 = 600_000;
const IDLE_MS: u64 = 5_000;
const SHORT_MS: u64 = 1_500;
if budget == 0 {
return (IDLE_MS, u64::MAX);
}
let share = |percent: u64| budget / 100 * percent;
if used < share(88) {
let step = budget / 100;
let aim = line.saturating_sub(5 * step);
if used < line.saturating_sub(2 * step) {
(KEPT_MS, u64::MAX)
} else if used < share(95) {
(IDLE_MS, used - share(85))
} else if used < line + 5 * step {
(IDLE_MS, used.saturating_sub(aim))
} else {
(SHORT_MS, used - share(85))
(SHORT_MS, used.saturating_sub(aim))
}
}
@@ -8849,16 +8860,21 @@ impl Meter {
tracing::info!(
"the texture queue's room: {used} MB in use - {blocks} MB free in texture blocks + {buffers} MB kept for \
card copies of title buffers + {targets} MB kept for render targets = {weighed} MB against a line of \
{line} MB ({} % of a budget of {budget} MB): {}",
crate::resource::room_percent(),
{line} MB ({:.1} % of a budget of {budget} MB): {}",
line as f64 * 100.0 / budget.max(1) as f64,
if weighed < line { "room" } else { "no room, new textures wait" }
);
}
let [looks, mapped, same, made, all] =
[0, 1, 2, 3, 4].map(|i| crate::draw::HEAP_TEXTURE_MICROS[i].swap(0, Ordering::Relaxed) / 1000);
let [looks, mapped, same, staged, all, made, views] =
[0, 1, 2, 3, 4, 5, 6].map(|i| crate::draw::HEAP_TEXTURE_MICROS[i].swap(0, Ordering::Relaxed) / 1000);
if all > 0 {
tracing::info!(
"the pass over textures picked by index, in the last 5 seconds: {all} ms in all, of them {looks} ms looking again at slots uploaded whole, {mapped} ms asking what of a texture's memory is mapped, {same} ms asking whether a host copy holds what memory holds, {made} ms making host copies and staging uploads; uploaded since the start: {} textures, {} MB",
"the pass over textures picked by index, in the last 5 seconds: {all} ms in all, of them {looks} ms looking \
again at slots uploaded whole, {mapped} ms asking what of a texture's memory is mapped, {same} ms asking \
whether a host copy holds what memory holds, {made} ms making host copies (creation and residency), \
{views} ms making the slots' views, {staged} ms staging uploads, {} ms the rest; uploaded since the start: \
{} textures, {} MB",
all.saturating_sub(looks + mapped + same + staged + made + views),
crate::draw::HEAP_TEXTURES.load(Ordering::Relaxed),
crate::draw::HEAP_TEXTURE_BYTES.load(Ordering::Relaxed) >> 20
);
@@ -10691,6 +10707,11 @@ mod batch { // Batch::texture_due lives here; `mod tests` is already taken at
assert_eq!((ms, bytes), (5_000, 5 * gib));
assert_eq!(vram_keep(100 * gib, 120 * gib), (1_500, 35 * gib));
assert_eq!(vram_keep(0, 7).0, 5_000);
// Around a line of 93 % instead: kept under 91 %, down to 88 %.
use super::vram_keep_below;
assert_eq!(vram_keep_below(100 * gib, 90 * gib, 93 * gib).0, 600_000);
assert_eq!(vram_keep_below(100 * gib, 92 * gib, 93 * gib), (5_000, 4 * gib));
assert_eq!(vram_keep_below(100 * gib, 99 * gib, 93 * gib), (1_500, 11 * gib));
}
#[test]
+44 -3
View File
@@ -1825,7 +1825,7 @@ pub(crate) fn room(memory: Option<[u64; 4]>) -> bool {
// used.
Some([budget, used, ..]) => {
used.saturating_sub(crate::pool::texture_room()) + crate::shadow::card_room_kept() + targets_room_kept()
< budget / 100 * room_percent()
< room_line(budget)
}
None => filled < WITHOUT_BUDGET,
}
@@ -1934,7 +1934,7 @@ pub(crate) fn room_terms(memory: Option<[u64; 4]>) -> Option<[u64; 6]> {
crate::pool::texture_room(),
crate::shadow::card_room_kept(),
targets_room_kept(),
budget / 100 * room_percent(),
room_line(budget),
]
.map(|bytes| bytes >> 20),
)
@@ -1965,6 +1965,8 @@ pub(crate) fn room_lately() -> bool {
/// textures wait (runs 170-174); the card copies of title buffers from
/// 1 MiB up take 449 MB more of the card than before (941 against 492),
/// and 94 gives the textures about that much back.
///
/// Unless that switch is set the line is [`line_for`] the budget instead.
#[cfg(windows)]
pub(crate) fn room_percent() -> u64 {
static PERCENT: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
@@ -1976,6 +1978,31 @@ pub(crate) fn room_percent() -> u64 {
})
}
/// The line [`room`] weighs the card's use against, in bytes of a budget
/// of `budget`: `VICTORY_TEXTURE_ROOM_PERCENT`'s share of it if set, else
/// [`line_for`] it.
#[cfg(windows)]
pub(crate) fn room_line(budget: u64) -> u64 {
static SET: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if *SET.get_or_init(|| std::env::var_os("VICTORY_TEXTURE_ROOM_PERCENT").is_some()) {
budget / 100 * room_percent()
} else {
line_for(budget)
}
}
/// The budget less what is left free for the host's own needs that are
/// made with no room test (render target bursts, buffer copies, upload
/// buffers): 768 MiB or a twentieth of the budget, whichever is more; never
/// under 90 % of it. A flat 90 % left 1,060 MB of a 10,600 MB budget unused
/// while new textures waited (2026-11-26 16:52 run: "no room" at 9,526 to
/// 9,881 MB weighed against 9,519 to 9,583); this gives about 290 MB of it
/// to textures there, and keeps 90 % on cards of 7.5 GB or less.
#[cfg(any(windows, test))]
fn line_for(budget: u64) -> u64 {
budget.saturating_sub((768u64 << 20).max(budget / 20)).max(budget / 100 * 90)
}
/// For the card accounting line ([`card_accounting`]): the title texture
/// made for a view of memory at `address` with the resource description
/// `desc`, if there is one ([`texture_at`] makes it; this does not).
@@ -2244,7 +2271,7 @@ pub(crate) fn let_go_idle() {
let used = used.saturating_sub(crate::pool::texture_room())
+ crate::shadow::card_room_kept()
+ targets_room_kept();
crate::host::vram_keep(budget, used)
crate::host::vram_keep_below(budget, used, room_line(budget))
}
_ => (crate::host::vram_idle_ms(memory), u64::MAX),
};
@@ -4750,6 +4777,20 @@ float4 lit() : SV_Target { return 1.0; }
assert_eq!(texture_floor_bytes(Some(32 * GIB)), 2 * GIB);
}
/// The texture queue's line: the budget less 768 MiB or 5 %, never under
/// 90 % of it.
#[test]
fn room_line_follows_the_budget() {
const MIB: u64 = 1 << 20;
// 10,600 MB: 768 MiB free (92.8 %), not 1,060 MB.
assert_eq!(line_for(10_600 * MIB), 9_832 * MIB);
// 24 GB: 5 % free.
assert_eq!(line_for(24_000 * MIB), 22_800 * MIB);
// 6 GB: 90 % (768 MiB would be more than a tenth).
assert_eq!(line_for(6_000 * MIB), 6_000 * MIB / 100 * 90);
assert_eq!(line_for(0), 0);
}
/// The room kept for render targets goes once the world's targets are
/// made and have stopped growing, and comes back when they grow again.
#[test]
+21 -1
View File
@@ -753,6 +753,25 @@ pub(crate) struct Digest(u128);
/// [`upload`] of the levels from `first` on, the ones a view shows: of a
/// texture the title placed there may be no memory of the larger ones, and
/// what lies there is another texture's. (After victory-amd 4436f8f1.)
/// Whether `list` has recorded [`upload_from`]'s upload of `resource`
/// from level `first` (or been asked to: see [`UPLOAD_STAGED`]).
pub(crate) fn upload_listed(list: &HostList, resource: This, first: u32) -> bool {
let first = if crate::resource::held_only() { first } else { 0 };
list.used(upload_key(resource, first))
}
/// The key [`upload_from`] asks `HostList::first_use` with.
fn upload_key(resource: This, first: u32) -> usize {
1 << 63 | (resource as usize) << 4 | first as usize & 15
}
thread_local! {
/// Set by [`upload_from`] when it found or made the texture's staging
/// buffer (its upload is recorded or checked at submission): for a
/// caller that must know the upload happened.
pub(crate) static UPLOAD_STAGED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
pub(crate) fn upload_from(list: &HostList, resource: This, first: u32) {
let first = if crate::resource::held_only() { first } else { 0 };
let host_written = HOST_WRITTEN
@@ -767,7 +786,7 @@ pub(crate) fn upload_from(list: &HostList, resource: This, first: u32) {
}
// Once a list for each first level; not again for one that the list's
// upload of larger ones held too.
if !list.first_use(1 << 63 | (resource as usize) << 4 | first as usize & 15) {
if !list.first_use(upload_key(resource, first)) {
return;
}
if !list.first_use(resource as usize) && staged_from(resource) <= first {
@@ -781,6 +800,7 @@ pub(crate) fn upload_from(list: &HostList, resource: This, first: u32) {
let Some(staging) = staging(resource, &copy, first) else {
return;
};
UPLOAD_STAGED.set(true);
use std::sync::atomic::Ordering::Relaxed;
let baking = BAKING.get();
if baking {