victory-gta6: this fork's changes on top of VIctory main (ffe310e)
World rendering, lighting, frame-rate and memory work for Grand Theft Auto VI (Xbox Series X profile) on one Windows PC with an NVIDIA RTX 4070. The README describes the state, the changes and the credits. Based on VIctory by Marvisak and contributors (GPL-3.0-or-later); XCL by ivall and contributors was used as a reference. No game files are included. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
1 parent
45c6e80601
commit
6fa814fba5
61 files changed
+32259
-1274
No files matched your search
@@ -16,7 +16,8 @@
|
||||
//! guess; a pass the title runs only once and whose pipeline takes longer
|
||||
//! is lost.
|
||||
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};
|
||||
use std::sync::{Arc, Condvar, Mutex, OnceLock};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -27,7 +28,7 @@ use super::host::{GraphicsState, HostPipeline};
|
||||
pub(crate) struct Pending {
|
||||
shaders: Vec<Arc<Shader>>,
|
||||
pub(crate) graphics: Option<Box<GraphicsState>>,
|
||||
queued: AtomicBool,
|
||||
queue_state: AtomicU8,
|
||||
made: OnceLock<Option<HostPipeline>>,
|
||||
/// What identifies it across runs (shader containers and state), and
|
||||
/// whether a draw has used it yet.
|
||||
@@ -52,7 +53,7 @@ impl Pending {
|
||||
Pending {
|
||||
shaders,
|
||||
graphics,
|
||||
queued: AtomicBool::new(false),
|
||||
queue_state: AtomicU8::new(NOT_QUEUED),
|
||||
made: OnceLock::new(),
|
||||
key: key.finalize().to_hex()[..32].to_owned(),
|
||||
used: AtomicBool::new(false),
|
||||
@@ -109,22 +110,25 @@ pub(crate) fn waiting() -> u64 {
|
||||
/// queues it on first use.
|
||||
pub(crate) fn ready(p: &Arc<Pending>) -> Option<&HostPipeline> {
|
||||
if p.made.get().is_none() {
|
||||
start(p);
|
||||
start(p, REQUESTED);
|
||||
wait(p, crate::frame::current());
|
||||
}
|
||||
let made = p.made.get()?.as_ref();
|
||||
if made.is_some() && !p.used.swap(true, Ordering::Relaxed) {
|
||||
// Also one that could not be made (a shader that does not translate):
|
||||
// the next run then finds that out while loading, like the others,
|
||||
// instead of in the world's frames, where each first use of such a
|
||||
// pipeline waits on the frame's budget for nothing.
|
||||
if !p.used.swap(true, Ordering::Relaxed) {
|
||||
super::library::note_used(&p.key);
|
||||
}
|
||||
made
|
||||
}
|
||||
|
||||
/// Queues `p` for the workers, once.
|
||||
fn start(p: &Arc<Pending>) {
|
||||
if !p.queued.swap(true, Ordering::Relaxed) {
|
||||
PEAK.fetch_max(WAITING.fetch_add(1, Ordering::Relaxed) + 1, Ordering::Relaxed);
|
||||
queue(p.clone());
|
||||
}
|
||||
/// Makes `p` on this thread if it is not made yet (waiting for a worker
|
||||
/// that is making it); whether it has a host pipeline.
|
||||
pub(crate) fn now(p: &Arc<Pending>) -> bool {
|
||||
p.make();
|
||||
p.made.get().is_some_and(|made| made.is_some())
|
||||
}
|
||||
|
||||
/// At the title's creation of a pipeline: starts it at once if an earlier
|
||||
@@ -132,18 +136,43 @@ fn start(p: &Arc<Pending>) {
|
||||
pub(crate) fn early(p: &Arc<Pending>) {
|
||||
if !p.shaders.is_empty() && super::library::wanted_early(&p.key) {
|
||||
super::library::EARLY.fetch_add(1, Ordering::Relaxed);
|
||||
start(p);
|
||||
start(p, EARLY);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the frame's budget is one for all threads together: a thread
|
||||
/// that waits takes what is left of it and gives back what it did not
|
||||
/// use. Counted only when a wait had ended, the budget was one per thread:
|
||||
/// each of the title's five recording threads began its first wait of a
|
||||
/// frame with nothing counted yet and waited the whole half second, 2.5 s
|
||||
/// of waiting in every frame in which pipelines were being made (runs
|
||||
/// 34-49: "2504 ms waiting for pipelines" in each such frame).
|
||||
/// On by default; `VICTORY_PIPELINE_WAIT_SHARED=0` counts as before.
|
||||
fn shared_budget() -> bool {
|
||||
static ON: OnceLock<bool> = OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var_os("VICTORY_PIPELINE_WAIT_SHARED").is_none_or(|v| v != "0"))
|
||||
}
|
||||
|
||||
/// Waits for `p` while frame `frame` has budget left.
|
||||
fn wait(p: &Pending, frame: u64) {
|
||||
static FRAME: AtomicU64 = AtomicU64::new(u64::MAX);
|
||||
static WAITED_US: AtomicU64 = AtomicU64::new(0);
|
||||
if FRAME.swap(frame, Ordering::Relaxed) != frame {
|
||||
WAITED_US.store(0, Ordering::Relaxed);
|
||||
// The frame the budget belongs to, and what was waited of it (or is
|
||||
// taken by a thread waiting now).
|
||||
static SPENT: Mutex<(u64, Duration)> = Mutex::new((u64::MAX, Duration::ZERO));
|
||||
let shared = shared_budget();
|
||||
let left = {
|
||||
let mut spent = SPENT.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if spent.0 != frame {
|
||||
*spent = (frame, Duration::ZERO);
|
||||
}
|
||||
let left = BUDGET.saturating_sub(spent.1);
|
||||
if shared {
|
||||
spent.1 += left;
|
||||
}
|
||||
left
|
||||
};
|
||||
if left.is_zero() {
|
||||
return;
|
||||
}
|
||||
let left = BUDGET.saturating_sub(Duration::from_micros(WAITED_US.load(Ordering::Relaxed)));
|
||||
let started = Instant::now();
|
||||
let (lock, done) = &DONE;
|
||||
let mut held = lock.lock().unwrap_or_else(|e| e.into_inner());
|
||||
@@ -154,22 +183,115 @@ fn wait(p: &Pending, frame: u64) {
|
||||
}
|
||||
held = done.wait_timeout(held, rest).unwrap_or_else(|e| e.into_inner()).0;
|
||||
}
|
||||
WAITED_US.fetch_add(started.elapsed().as_micros() as u64, Ordering::Relaxed);
|
||||
drop(held);
|
||||
let waited = started.elapsed().min(left);
|
||||
{
|
||||
let mut spent = SPENT.lock().unwrap_or_else(|e| e.into_inner());
|
||||
// A frame that began meanwhile has its own budget.
|
||||
if spent.0 == frame {
|
||||
if shared {
|
||||
spent.1 = spent.1.saturating_sub(left - waited);
|
||||
} else {
|
||||
spent.1 += waited;
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "diagnostics")]
|
||||
crate::copystat::add(
|
||||
crate::copystat::C::PipelineWaitUs,
|
||||
started.elapsed().as_micros() as u64,
|
||||
);
|
||||
}
|
||||
|
||||
/// Hands `p` to the workers (half the logical processors, 2 to 8), started
|
||||
/// on first use.
|
||||
fn queue(p: Arc<Pending>) {
|
||||
static SENDER: OnceLock<std::sync::mpsc::Sender<Arc<Pending>>> = OnceLock::new();
|
||||
let sender = SENDER.get_or_init(|| {
|
||||
let (sender, receiver) = std::sync::mpsc::channel::<Arc<Pending>>();
|
||||
let receiver = Arc::new(Mutex::new(receiver));
|
||||
const NOT_QUEUED: u8 = 0;
|
||||
const EARLY: u8 = 1;
|
||||
const REQUESTED: u8 = 2;
|
||||
const MAKING: u8 = 3;
|
||||
|
||||
struct Queue {
|
||||
early: VecDeque<Arc<Pending>>,
|
||||
requested: VecDeque<Arc<Pending>>,
|
||||
}
|
||||
|
||||
impl Queue {
|
||||
const fn new() -> Self {
|
||||
Self {
|
||||
early: VecDeque::new(),
|
||||
requested: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Queues an unrequested pipeline or promotes one queued at creation.
|
||||
/// Returns whether it is new to the queue, or `None` if nothing changed.
|
||||
fn push(&mut self, p: &Arc<Pending>, priority: u8) -> Option<bool> {
|
||||
let prev = p.queue_state.load(Ordering::Relaxed);
|
||||
if prev >= priority {
|
||||
return None;
|
||||
}
|
||||
p.queue_state.store(priority, Ordering::Relaxed);
|
||||
if priority == REQUESTED {
|
||||
self.requested.push_back(p.clone());
|
||||
} else {
|
||||
self.early.push_back(p.clone());
|
||||
}
|
||||
Some(prev == NOT_QUEUED)
|
||||
}
|
||||
|
||||
fn pop(&mut self) -> Option<Arc<Pending>> {
|
||||
let p = self.requested.pop_front().or_else(|| {
|
||||
// An early entry can have been promoted and claimed already.
|
||||
std::iter::from_fn(|| self.early.pop_front())
|
||||
.find(|p| p.queue_state.load(Ordering::Relaxed) == EARLY)
|
||||
})?;
|
||||
p.queue_state.store(MAKING, Ordering::Relaxed);
|
||||
Some(p)
|
||||
}
|
||||
}
|
||||
|
||||
static QUEUE: Mutex<Queue> = Mutex::new(Queue::new());
|
||||
static AVAILABLE: Condvar = Condvar::new();
|
||||
|
||||
/// Start or promote work for a draw without waiting behind the entire early
|
||||
/// list. Queue changes and worker claims happen under the same lock.
|
||||
fn start(p: &Arc<Pending>, priority: u8) {
|
||||
if p.queue_state.load(Ordering::Relaxed) >= priority {
|
||||
return;
|
||||
}
|
||||
if !workers() {
|
||||
if p.queue_state.swap(MAKING, Ordering::Relaxed) == NOT_QUEUED {
|
||||
p.make();
|
||||
}
|
||||
return;
|
||||
}
|
||||
let mut queue = QUEUE.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if let Some(new) = queue.push(p, priority) {
|
||||
if new {
|
||||
PEAK.fetch_max(WAITING.fetch_add(1, Ordering::Relaxed) + 1, Ordering::Relaxed);
|
||||
}
|
||||
AVAILABLE.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
fn next() -> Arc<Pending> {
|
||||
let mut queue = QUEUE.lock().unwrap_or_else(|e| e.into_inner());
|
||||
loop {
|
||||
if let Some(p) = queue.pop() {
|
||||
return p;
|
||||
}
|
||||
queue = AVAILABLE.wait(queue).unwrap_or_else(|e| e.into_inner());
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts the workers (half the logical processors, 2 to 8) on first use.
|
||||
fn workers() -> bool {
|
||||
static RUNNING: OnceLock<bool> = OnceLock::new();
|
||||
*RUNNING.get_or_init(|| {
|
||||
let mut running = false;
|
||||
let workers = std::thread::available_parallelism().map_or(2, |n| (n.get() / 2).clamp(2, 8));
|
||||
for i in 0..workers {
|
||||
let receiver = receiver.clone();
|
||||
let spawned = std::thread::Builder::new()
|
||||
.name(format!("victory-pipelines-{i}"))
|
||||
.spawn(move || {
|
||||
.spawn(|| {
|
||||
// Below the title's own threads (loading, recording).
|
||||
// SAFETY: a plain call on our own thread.
|
||||
unsafe {
|
||||
@@ -177,9 +299,7 @@ fn queue(p: Arc<Pending>) {
|
||||
let _ = SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
|
||||
}
|
||||
loop {
|
||||
let next = receiver.lock().unwrap_or_else(|e| e.into_inner()).recv();
|
||||
let Ok(p) = next else { return };
|
||||
p.make();
|
||||
next().make();
|
||||
if WAITING.fetch_sub(1, Ordering::Relaxed) == 1 {
|
||||
drained();
|
||||
} else {
|
||||
@@ -189,15 +309,12 @@ fn queue(p: Arc<Pending>) {
|
||||
});
|
||||
if let Err(e) = spawned {
|
||||
tracing::warn!(error = %e, "no pipeline worker");
|
||||
} else {
|
||||
running = true;
|
||||
}
|
||||
}
|
||||
sender
|
||||
});
|
||||
if let Err(e) = sender.send(p) {
|
||||
// No worker: made here instead.
|
||||
e.0.make();
|
||||
WAITING.fetch_sub(1, Ordering::Relaxed);
|
||||
}
|
||||
running
|
||||
})
|
||||
}
|
||||
|
||||
/// The most pipelines waiting at once since the workers last ran out.
|
||||
@@ -228,11 +345,31 @@ mod tests {
|
||||
fn first_use_is_made_by_a_worker() {
|
||||
let p = Arc::new(Pending::new(Vec::new(), None));
|
||||
assert!(ready(&p).is_none());
|
||||
assert!(p.queued.load(Ordering::Relaxed));
|
||||
assert_ne!(p.queue_state.load(Ordering::Relaxed), NOT_QUEUED);
|
||||
let started = Instant::now();
|
||||
while p.made.get().is_none() && started.elapsed() < Duration::from_secs(10) {
|
||||
std::thread::yield_now();
|
||||
}
|
||||
assert!(matches!(p.made.get(), Some(None)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn draws_promote_early_pipelines_without_claiming_them_twice() {
|
||||
let earlier = Arc::new(Pending::new(Vec::new(), None));
|
||||
let promoted = Arc::new(Pending::new(Vec::new(), None));
|
||||
let requested = Arc::new(Pending::new(Vec::new(), None));
|
||||
let mut queue = Queue::new();
|
||||
|
||||
assert_eq!(queue.push(&earlier, EARLY), Some(true));
|
||||
assert_eq!(queue.push(&promoted, EARLY), Some(true));
|
||||
assert_eq!(queue.push(&requested, REQUESTED), Some(true));
|
||||
assert_eq!(queue.push(&promoted, REQUESTED), Some(false));
|
||||
assert_eq!(queue.push(&promoted, REQUESTED), None);
|
||||
|
||||
assert!(Arc::ptr_eq(&queue.pop().unwrap(), &requested));
|
||||
assert!(Arc::ptr_eq(&queue.pop().unwrap(), &promoted));
|
||||
assert_eq!(queue.push(&promoted, REQUESTED), None);
|
||||
assert!(Arc::ptr_eq(&queue.pop().unwrap(), &earlier));
|
||||
assert!(queue.pop().is_none());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user