Passes over the screen have pipeline workers and a wait of their own (VICTORY_SCREEN_WAIT_MS=0: not); after Belobug 18e57db8
The first run after a new translation (textures-d2: 2,500 pipelines being made, 594 draws a frame dropped in the world's first minute) had no lamps and no colour in the shade to its end; the second (textures-d3) had them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
ac11bbbe5f
commit
a98966866c
2 files changed
+102
-20
No files matched your search
@@ -151,6 +151,17 @@ fn host_blend(blend: &D3D12_BLEND_DESC, targets: &D3D12_RT_FORMAT_ARRAY) -> D3D1
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GraphicsState {
|
impl GraphicsState {
|
||||||
|
/// Whether the draws are a pass over the screen: no vertex input, no
|
||||||
|
/// depth or stencil, a target. Such passes fill tables once; their shaders
|
||||||
|
/// are few. (After Belobug/VIctory-Belobug-AMD 18e57db8.)
|
||||||
|
pub(crate) fn screen_pass(&self) -> bool {
|
||||||
|
self.inputs.is_empty()
|
||||||
|
&& (self.depth_format == DXGI_FORMAT_UNKNOWN
|
||||||
|
|| !self.depth.DepthEnable.as_bool() && !self.depth.StencilEnable.as_bool())
|
||||||
|
&& self.targets.NumRenderTargets > 0
|
||||||
|
&& self.topology == D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE
|
||||||
|
}
|
||||||
|
|
||||||
/// Takes in one subobject of a stream: its type, then its data at +4
|
/// Takes in one subobject of a stream: its type, then its data at +4
|
||||||
/// (+8 for those holding pointers).
|
/// (+8 for those holding pointers).
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -13,7 +13,13 @@
|
|||||||
//! whose pipeline is not ready is dropped, as an untranslated one is.
|
//! whose pipeline is not ready is dropped, as an untranslated one is.
|
||||||
//!
|
//!
|
||||||
//! The default budget is half a second per title frame. A pass the title
|
//! The default budget is half a second per title frame. A pass the title
|
||||||
//! runs only once and whose pipeline takes longer is lost.
|
//! runs only once and whose pipeline takes longer is lost. Passes over the
|
||||||
|
//! screen (no vertex input, no depth) have workers and a longer budget of
|
||||||
|
//! their own ([`screen_budget`]): the title fills its lighting tables with
|
||||||
|
//! such draws once, at the world's first frame, and behind a world's worth of
|
||||||
|
//! pipelines they were dropped; that run had no lamps and no diffuse light to
|
||||||
|
//! its end (run textures-d2, the first after a new translation; Belobug
|
||||||
|
//! 18e57db8 found the same).
|
||||||
|
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};
|
||||||
@@ -33,6 +39,8 @@ pub(crate) struct Pending {
|
|||||||
/// whether a draw or dispatch has requested it yet.
|
/// whether a draw or dispatch has requested it yet.
|
||||||
key: String,
|
key: String,
|
||||||
used: AtomicBool,
|
used: AtomicBool,
|
||||||
|
/// A pass over the screen ([`GraphicsState::screen_pass`]).
|
||||||
|
screen: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
// SAFETY: D3D12 objects are free-threaded; the graphics state (whose input
|
// SAFETY: D3D12 objects are free-threaded; the graphics state (whose input
|
||||||
@@ -50,6 +58,7 @@ impl Pending {
|
|||||||
}
|
}
|
||||||
key.update(graphics.as_ref().map(|g| g.describe()).unwrap_or_default().as_bytes());
|
key.update(graphics.as_ref().map(|g| g.describe()).unwrap_or_default().as_bytes());
|
||||||
Pending {
|
Pending {
|
||||||
|
screen: graphics.as_ref().is_some_and(|g| g.screen_pass()),
|
||||||
shaders,
|
shaders,
|
||||||
graphics,
|
graphics,
|
||||||
queue_state: AtomicU8::new(NOT_QUEUED),
|
queue_state: AtomicU8::new(NOT_QUEUED),
|
||||||
@@ -113,6 +122,21 @@ fn wait_budget() -> Duration {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// As [`wait_budget`], for passes over the screen (`VICTORY_SCREEN_WAIT_MS`,
|
||||||
|
/// 10 s; 0: like the others). Below the title's 30 s watchdog.
|
||||||
|
fn screen_budget() -> Duration {
|
||||||
|
static BUDGET: OnceLock<Duration> = OnceLock::new();
|
||||||
|
*BUDGET.get_or_init(|| {
|
||||||
|
let millis = std::env::var("VICTORY_SCREEN_WAIT_MS").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(10_000);
|
||||||
|
Duration::from_millis(millis)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether passes over the screen go before the others.
|
||||||
|
fn screens_first() -> bool {
|
||||||
|
!screen_budget().is_zero()
|
||||||
|
}
|
||||||
|
|
||||||
/// Pipelines queued and not made yet.
|
/// Pipelines queued and not made yet.
|
||||||
static WAITING: AtomicU64 = AtomicU64::new(0);
|
static WAITING: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
@@ -167,16 +191,18 @@ fn shared_budget() -> bool {
|
|||||||
|
|
||||||
/// Waits for `p` while frame `frame` has budget left.
|
/// Waits for `p` while frame `frame` has budget left.
|
||||||
fn wait(p: &Pending, frame: u64) {
|
fn wait(p: &Pending, frame: u64) {
|
||||||
let budget = wait_budget();
|
let screen = p.screen && screens_first();
|
||||||
|
let budget = if screen { screen_budget() } else { wait_budget() };
|
||||||
if budget.is_zero() {
|
if budget.is_zero() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// The frame the budget belongs to, and what was waited of it (or is
|
// The frame the budget belongs to, and what was waited of it (or is
|
||||||
// taken by a thread waiting now).
|
// taken by a thread waiting now); of the passes over the screen too.
|
||||||
static SPENT: Mutex<(u64, Duration)> = Mutex::new((u64::MAX, Duration::ZERO));
|
static SPENT: [Mutex<(u64, Duration)>; 2] = [const { Mutex::new((u64::MAX, Duration::ZERO)) }; 2];
|
||||||
|
let spent_of = &SPENT[usize::from(screen)];
|
||||||
let shared = shared_budget();
|
let shared = shared_budget();
|
||||||
let left = {
|
let left = {
|
||||||
let mut spent = SPENT.lock().unwrap_or_else(|e| e.into_inner());
|
let mut spent = spent_of.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
if spent.0 != frame {
|
if spent.0 != frame {
|
||||||
*spent = (frame, Duration::ZERO);
|
*spent = (frame, Duration::ZERO);
|
||||||
}
|
}
|
||||||
@@ -201,8 +227,16 @@ fn wait(p: &Pending, frame: u64) {
|
|||||||
}
|
}
|
||||||
drop(held);
|
drop(held);
|
||||||
let waited = started.elapsed().min(left);
|
let waited = started.elapsed().min(left);
|
||||||
|
if screen && waited > Duration::from_millis(500) {
|
||||||
|
tracing::info!(
|
||||||
|
ms = waited.as_millis() as u64,
|
||||||
|
made = p.made.get().is_some(),
|
||||||
|
shaders = ?p.shaders,
|
||||||
|
"a pass over the screen was waited for (VICTORY_SCREEN_WAIT_MS=0: dropped past half a second a frame)"
|
||||||
|
);
|
||||||
|
}
|
||||||
{
|
{
|
||||||
let mut spent = SPENT.lock().unwrap_or_else(|e| e.into_inner());
|
let mut spent = spent_of.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
// A frame that began meanwhile has its own budget.
|
// A frame that began meanwhile has its own budget.
|
||||||
if spent.0 == frame {
|
if spent.0 == frame {
|
||||||
if shared {
|
if shared {
|
||||||
@@ -225,6 +259,9 @@ const REQUESTED: u8 = 2;
|
|||||||
const MAKING: u8 = 3;
|
const MAKING: u8 = 3;
|
||||||
|
|
||||||
struct Queue {
|
struct Queue {
|
||||||
|
/// Requested passes over the screen: before all others, and the only ones
|
||||||
|
/// [`SCREEN_WORKERS`] take.
|
||||||
|
screen: VecDeque<Arc<Pending>>,
|
||||||
early: VecDeque<Arc<Pending>>,
|
early: VecDeque<Arc<Pending>>,
|
||||||
requested: VecDeque<Arc<Pending>>,
|
requested: VecDeque<Arc<Pending>>,
|
||||||
}
|
}
|
||||||
@@ -232,6 +269,7 @@ struct Queue {
|
|||||||
impl Queue {
|
impl Queue {
|
||||||
const fn new() -> Self {
|
const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
screen: VecDeque::new(),
|
||||||
early: VecDeque::new(),
|
early: VecDeque::new(),
|
||||||
requested: VecDeque::new(),
|
requested: VecDeque::new(),
|
||||||
}
|
}
|
||||||
@@ -245,7 +283,9 @@ impl Queue {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
p.queue_state.store(priority, Ordering::Relaxed);
|
p.queue_state.store(priority, Ordering::Relaxed);
|
||||||
if priority == REQUESTED {
|
if priority == REQUESTED && p.screen && screens_first() {
|
||||||
|
self.screen.push_back(p.clone());
|
||||||
|
} else if priority == REQUESTED {
|
||||||
self.requested.push_back(p.clone());
|
self.requested.push_back(p.clone());
|
||||||
} else {
|
} else {
|
||||||
self.early.push_back(p.clone());
|
self.early.push_back(p.clone());
|
||||||
@@ -253,12 +293,16 @@ impl Queue {
|
|||||||
Some(prev == NOT_QUEUED)
|
Some(prev == NOT_QUEUED)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pop(&mut self) -> Option<Arc<Pending>> {
|
fn pop(&mut self, screens_only: bool) -> Option<Arc<Pending>> {
|
||||||
let p = self.requested.pop_front().or_else(|| {
|
let mut p = self.screen.pop_front();
|
||||||
|
if p.is_none() && !screens_only {
|
||||||
|
p = self.requested.pop_front().or_else(|| {
|
||||||
// An early entry can have been promoted and claimed already.
|
// An early entry can have been promoted and claimed already.
|
||||||
std::iter::from_fn(|| self.early.pop_front())
|
std::iter::from_fn(|| self.early.pop_front())
|
||||||
.find(|p| p.queue_state.load(Ordering::Relaxed) == EARLY)
|
.find(|p| p.queue_state.load(Ordering::Relaxed) == EARLY)
|
||||||
})?;
|
});
|
||||||
|
}
|
||||||
|
let p = p?;
|
||||||
p.queue_state.store(MAKING, Ordering::Relaxed);
|
p.queue_state.store(MAKING, Ordering::Relaxed);
|
||||||
Some(p)
|
Some(p)
|
||||||
}
|
}
|
||||||
@@ -284,14 +328,21 @@ fn start(p: &Arc<Pending>, priority: u8) {
|
|||||||
if new {
|
if new {
|
||||||
PEAK.fetch_max(WAITING.fetch_add(1, Ordering::Relaxed) + 1, Ordering::Relaxed);
|
PEAK.fetch_max(WAITING.fetch_add(1, Ordering::Relaxed) + 1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
if p.screen {
|
||||||
|
AVAILABLE.notify_all();
|
||||||
|
} else {
|
||||||
AVAILABLE.notify_one();
|
AVAILABLE.notify_one();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next() -> Arc<Pending> {
|
/// Workers that make passes over the screen alone, besides the others.
|
||||||
|
const SCREEN_WORKERS: usize = 2;
|
||||||
|
|
||||||
|
fn next(screens_only: bool) -> Arc<Pending> {
|
||||||
let mut queue = QUEUE.lock().unwrap_or_else(|e| e.into_inner());
|
let mut queue = QUEUE.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
loop {
|
loop {
|
||||||
if let Some(p) = queue.pop() {
|
if let Some(p) = queue.pop(screens_only) {
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
queue = AVAILABLE.wait(queue).unwrap_or_else(|e| e.into_inner());
|
queue = AVAILABLE.wait(queue).unwrap_or_else(|e| e.into_inner());
|
||||||
@@ -320,14 +371,16 @@ fn workers() -> bool {
|
|||||||
std::thread::available_parallelism().map_or(2, |n| n.get()),
|
std::thread::available_parallelism().map_or(2, |n| n.get()),
|
||||||
);
|
);
|
||||||
tracing::info!(workers, "pipeline workers");
|
tracing::info!(workers, "pipeline workers");
|
||||||
for i in 0..workers {
|
let screens = if screens_first() { SCREEN_WORKERS } else { 0 };
|
||||||
|
for i in 0..workers + screens {
|
||||||
|
let screens_only = i >= workers;
|
||||||
let spawned = std::thread::Builder::new()
|
let spawned = std::thread::Builder::new()
|
||||||
.name(format!("victory-pipelines-{i}"))
|
.name(format!("victory-pipelines-{i}"))
|
||||||
.spawn(|| {
|
.spawn(move || {
|
||||||
// Below the title's own threads (loading, recording),
|
// Below the title's own threads (loading, recording),
|
||||||
// and the streaming one, which is below normal itself
|
// and the streaming one, which is below normal itself
|
||||||
// (`VICTORY_PIPELINE_LOWEST=0`: level with it, as before).
|
// (`VICTORY_PIPELINE_LOWEST=0`: level with it, as before).
|
||||||
let lowest = std::env::var_os("VICTORY_PIPELINE_LOWEST").is_none_or(|v| v != "0");
|
let lowest = !screens_only && std::env::var_os("VICTORY_PIPELINE_LOWEST").is_none_or(|v| v != "0");
|
||||||
// SAFETY: a plain call on our own thread.
|
// SAFETY: a plain call on our own thread.
|
||||||
unsafe {
|
unsafe {
|
||||||
use windows::Win32::System::Threading::*;
|
use windows::Win32::System::Threading::*;
|
||||||
@@ -335,7 +388,7 @@ fn workers() -> bool {
|
|||||||
let _ = SetThreadPriority(GetCurrentThread(), priority);
|
let _ = SetThreadPriority(GetCurrentThread(), priority);
|
||||||
}
|
}
|
||||||
loop {
|
loop {
|
||||||
next().make();
|
next(screens_only).make();
|
||||||
if WAITING.fetch_sub(1, Ordering::Relaxed) == 1 {
|
if WAITING.fetch_sub(1, Ordering::Relaxed) == 1 {
|
||||||
drained();
|
drained();
|
||||||
} else {
|
} else {
|
||||||
@@ -425,10 +478,28 @@ mod tests {
|
|||||||
assert_eq!(queue.push(&promoted, REQUESTED), Some(false));
|
assert_eq!(queue.push(&promoted, REQUESTED), Some(false));
|
||||||
assert_eq!(queue.push(&promoted, REQUESTED), None);
|
assert_eq!(queue.push(&promoted, REQUESTED), None);
|
||||||
|
|
||||||
assert!(Arc::ptr_eq(&queue.pop().unwrap(), &requested));
|
assert!(Arc::ptr_eq(&queue.pop(false).unwrap(), &requested));
|
||||||
assert!(Arc::ptr_eq(&queue.pop().unwrap(), &promoted));
|
assert!(Arc::ptr_eq(&queue.pop(false).unwrap(), &promoted));
|
||||||
assert_eq!(queue.push(&promoted, REQUESTED), None);
|
assert_eq!(queue.push(&promoted, REQUESTED), None);
|
||||||
assert!(Arc::ptr_eq(&queue.pop().unwrap(), &earlier));
|
assert!(Arc::ptr_eq(&queue.pop(false).unwrap(), &earlier));
|
||||||
assert!(queue.pop().is_none());
|
assert!(queue.pop(false).is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Passes over the screen go before all others, and workers of their own
|
||||||
|
/// take nothing else.
|
||||||
|
#[test]
|
||||||
|
fn passes_over_the_screen_go_first() {
|
||||||
|
let of = |screen| {
|
||||||
|
let mut p = Pending::new(Vec::new(), None);
|
||||||
|
p.screen = screen;
|
||||||
|
Arc::new(p)
|
||||||
|
};
|
||||||
|
let (world, screen) = (of(false), of(true));
|
||||||
|
let mut queue = Queue::new();
|
||||||
|
assert_eq!(queue.push(&world, REQUESTED), Some(true));
|
||||||
|
assert_eq!(queue.push(&screen, REQUESTED), Some(true));
|
||||||
|
assert!(Arc::ptr_eq(&queue.pop(true).unwrap(), &screen));
|
||||||
|
assert!(queue.pop(true).is_none());
|
||||||
|
assert!(Arc::ptr_eq(&queue.pop(false).unwrap(), &world));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user