Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/victory/src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ pub fn warm(paths: Vec<PathBuf>, file: &Path, jobs: Option<usize>) -> Result<()>
let counts = [const { AtomicUsize::new(0) }; 5];
for root in &paths {
for entry in walkdir::WalkDir::new(root).into_iter().filter_map(Result::ok) {
// Past the deadline the workers break out at once: walking and
// opening the rest of the assets would only scan for nothing.
if limit.is_some_and(|n| began.elapsed().as_secs() >= n) {
break;
}
if !entry.file_type().is_file() || entry.metadata().map_or(0, |m| m.len()) > MAX_FILE {
continue;
}
Expand Down
7 changes: 6 additions & 1 deletion crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const WINDOW_MOST: u64 = 64;
/// the list's scratch buffer in one pass, and the draws that follow read
/// their record there with no pass of ours and no barrier (the scratch
/// buffer stays readable as arguments until the next clamp), until one of
/// these: a compute
/// these: a nonempty compute
/// dispatch, a direct draw, an indirect call with a count buffer or more
/// than one record, a draw whose shaders can store to buffers, a record
/// outside the window, another host list.
Expand Down Expand Up @@ -2191,6 +2191,11 @@ impl State {

/// Runs a dispatch on the host.
pub(crate) fn dispatch(&mut self, list: &HostList, x: u32, y: u32, z: u32) {
// A zero-sized grid has no shader work: do not demand or wait for
// its pipeline.
if x == 0 || y == 0 || z == 0 {
return;
}
// It may write the arguments a window holds clamped.
self.window = None;
#[cfg(feature = "diagnostics")]
Expand Down
5 changes: 4 additions & 1 deletion crates/victory-gpu/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ pub(crate) extern "system" fn set_interval(
};
tracing::info!(?interval, "frame interval");
// A new interval starts from now, after the last tick handed out.
// Read it under the lock `next_tick` uses, so a concurrent wait cannot
// reserve a newer tick between the read and the clock replacement.
let mut clock = clock();
let first = LAST
.iter()
.map(|t| t.load(Ordering::Relaxed))
.max()
.unwrap_or(0);
*clock() = Some(Clock {
*clock = Some(Clock {
start: Instant::now(),
interval,
first,
Expand Down
27 changes: 26 additions & 1 deletion crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7100,7 +7100,31 @@ impl HostList {
fn run_deferred(&self, batch: &mut Batch) {
#[cfg(feature = "diagnostics")]
use crate::copystat::{C, add};
// Texture refreshes only read title memory and write each staging
// buffer's own memory. Consecutive ones can therefore run together;
// the group is still finished before the next step that writes title
// memory, preserving submission ordering.
let mut textures = Vec::new();
let run_textures = |textures: &mut Vec<std::sync::Arc<crate::texture::Staging>>| {
if textures.is_empty() {
return;
}
if wide_refresh() && textures.len() >= 4 {
let jobs: Vec<Box<dyn FnOnce() + Send>> = textures
.drain(..)
.map(|staging| Box::new(move || staging.refresh()) as Box<dyn FnOnce() + Send>)
.collect();
crate::shadow::wide_run(jobs);
} else {
for staging in textures.drain(..) {
staging.refresh();
}
}
};
for step in &self.deferred().copies {
if !matches!(step, Step::Texture(_)) {
run_textures(&mut textures);
}
let started = std::time::Instant::now();
let copied = match step {
&Step::Copy(dst, src, len) => {
Expand Down Expand Up @@ -7172,14 +7196,15 @@ impl HostList {
}
Step::Texture(staging) => {
if batch.texture_due(staging) {
staging.refresh();
textures.push(staging.clone());
}
continue;
}
};
preparing(0, started.elapsed().as_micros() as u64);
preparing(1, copied as u64);
}
run_textures(&mut textures);
}
}

Expand Down
36 changes: 36 additions & 0 deletions crates/victory-gpu/src/pipeline/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,40 @@ mod tests {
assert!(Arc::ptr_eq(&queue.pop().unwrap(), &earlier));
assert!(queue.pop().is_none());
}

#[test]
fn zero_group_dispatches_do_not_request_a_pipeline() {
use crate::object::{S_OK, slot};

let Some(host) = crate::host::host() else {
return;
};
let list = host.new_list().unwrap();
// A pending pipeline that cannot translate makes any accidental
// demand observable without depending on a shader's build time.
let mut title = std::ptr::null();
assert_eq!(
crate::pipeline::hand_out(
Vec::new(),
None,
&crate::pipeline::PIPELINE_IIDS[0],
&mut title,
),
S_OK
);
let pending = &crate::pipeline::pipeline(title).host;
let mut state = crate::draw::State::default();
state.set_pipeline(title);
let zero_demands = [(0, 1, 1), (1, 0, 1), (1, 1, 0)].map(|(x, y, z)| {
state.dispatch(&list, x, y, z);
pending.used.load(Ordering::Relaxed)
});
state.dispatch(&list, 1, 1, 1);
let nonempty_demand = pending.used.load(Ordering::Relaxed);
list.close();
slot::<extern "system" fn(crate::object::This) -> u32>(title, 0x10)(title);
assert_eq!(zero_demands, [false; 3]);
assert!(nonempty_demand);
}

}
48 changes: 43 additions & 5 deletions crates/victory-gpu/src/shader_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,41 @@ fn build() -> [u8; 32] {
/// the run will have (`victory_shader::emit::translation_fingerprint`).
pub fn warm(container: &[u8]) -> Result<bool, String> {
let cache = CACHE.get().ok_or("no shader cache")?;
// The translation depends on the view-index cap as at runtime.
crate::descriptor::host::probe_view_limit();
let ran = AtomicBool::new(false);
// The mode the runtime translates with on this machine: an emulated
// mesh shader's cache entry is another key than the native one's, so a
// warm-up in only the native mode left emulation hardware (no mesh
// shader support) to translate every mesh program on first use. Only a
// mesh program's container keys by it: everything else translates in
// the native mode whatever the host (`pipeline::translate`).
let mesh = if container_is_mesh(container)
&& crate::host::host().is_some_and(crate::host::Host::emulate_mesh_shaders)
{
MeshMode::Emulate
} else {
MeshMode::Native
};
let translate = |c: &[u8]| {
ran.store(true, Ordering::Relaxed);
victory_shader::emit::translate(c).map_err(|e| e.to_string())
victory_shader::emit::translate_with(c, mesh).map_err(|e| e.to_string())
};
cached(cache, &build(), MeshMode::Native, container, translate).map(|_| ran.load(Ordering::Relaxed))
cached(cache, &build(), mesh, container, translate).map(|_| ran.load(Ordering::Relaxed))
}

/// Whether the container's program is a mesh program: the only stage the
/// mesh mode keys and translates differently.
fn container_is_mesh(container: &[u8]) -> bool {
let Ok(c) = victory_shader::Container::parse(container) else {
return false;
};
let Some(dxil) = c.part(victory_shader::FourCC::DXIL) else {
return false;
};
victory_shader::native_programs(dxil.data)
.map(|ps| ps.iter().any(|p| p.stage == victory_shader::HwStage::Ms))
.unwrap_or(false)
}

/// The translation of a console shader container, from the cache when
Expand Down Expand Up @@ -147,9 +176,18 @@ fn cached(
ran: AtomicBool::new(false),
};
for _ in 0..2 {
let path = cache
.get_or_convert(&converter, &input)
.map_err(|e| format!("{e:#}"))?;
let path = match cache.get_or_convert(&converter, &input) {
Ok(path) => path,
Err(e) => {
// A cache that cannot write (a full or read-only disk) must
// not cost the shader: the translation is the same work the
// converter just did, returned uncached instead of dropping
// every draw of the program for the rest of the run.
tracing::debug!("shader cache write failed ({e:#}); the shader is not cached");
MISSES.fetch_add(1, Ordering::Relaxed);
return (converter.translate)(container);
}
};
if let Some(shader) = std::fs::read(&path).ok().and_then(|b| decode(&b)) {
let (counter, what) = if converter.ran.load(Ordering::Relaxed) {
(&MISSES, "translated and cached")
Expand Down
Loading