Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
53 changes: 46 additions & 7 deletions crates/xcl/src/gpu/recomp/pso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,49 @@ fn shader_cache() -> &'static Mutex<ShaderCache> {
/// `console_to_dxil_for` with the title's memory windows given (`xcl shaders --precompile`
/// fills the caches without a title: the same keys as at its run).
pub fn console_to_dxil_in(blob: &[u8], stage: Stage, sampler_base: u32, split_wave64: bool, windows: u32) -> Result<Compiled, String> {
let compile_one = || compile_console_shader(blob, stage, sampler_base, windows, split_wave64);
console_to_dxil_in_status(blob, stage, sampler_base, split_wave64, windows).map(|(c, _)| c)
}

/// Shared prefix emitted when a shader contains unsupported instructions (`!Translation::complete()`)
/// and persisted in `translated/v2/*.err`, so both cold and cached lookups classify incomplete
/// translations consistently.
const INCOMPLETE_TRANSLATION_PREFIX: &str = "incomplete translation, ";

/// Outcome of `console_to_dxil_in_outcome`: separates compiled/cached shaders from incomplete
/// translations (`!Translation::complete()`, including `.err`-cached hits) and hard failures.
pub enum CompileOutcome {
Compiled { compiled: Compiled, from_cache: bool },
Incomplete(String),
Failed(String),
}

/// Like `console_to_dxil_in_status`, classifying incomplete translations (`!Translation::complete()`)
/// separately from hard compilation/container failures.
pub fn console_to_dxil_in_outcome(
blob: &[u8],
stage: Stage,
sampler_base: u32,
split_wave64: bool,
windows: u32,
) -> CompileOutcome {
match console_to_dxil_in_status(blob, stage, sampler_base, split_wave64, windows) {
Ok((compiled, from_cache)) => CompileOutcome::Compiled { compiled, from_cache },
Err(e) if e.starts_with(INCOMPLETE_TRANSLATION_PREFIX) => CompileOutcome::Incomplete(e),
Err(e) => CompileOutcome::Failed(e),
}
}

/// Like `console_to_dxil_in`, also returning whether the result came from the in-memory or
/// translated disk cache (`true` = cache hit, `false` = newly translated/compiled).
pub fn console_to_dxil_in_status(blob: &[u8], stage: Stage, sampler_base: u32, split_wave64: bool, windows: u32) -> Result<(Compiled, bool), String> {
if !cache_allowed(stage, |name| std::env::var_os(name).is_some()) {
return compile_one().map(|(c, _)| c);
return compile_console_shader(blob, stage, sampler_base, windows, split_wave64).map(|(c, _)| (c, false));
}
let mut from_cache = true;
let compile_one = || {
from_cache = false;
compile_console_shader(blob, stage, sampler_base, windows, split_wave64)
};
let key = ShaderKey {
hash: crate::gpu::shader::fnv64(blob),
stage,
Expand All @@ -168,7 +207,7 @@ pub fn console_to_dxil_in(blob: &[u8], stage: Stage, sampler_base: u32, split_wa
// with `XCL_DIRECT_DXIL` the disk entries live under their own identity (backend tag,
// direct translator version, routing mode): never mixed with the HLSL path's entries
let from_disk = || translated::cached(&dxc::cache_dir(), blob, stage, sampler_base, windows, split_wave64, compile_one);
cached_shader(shader_cache(), key, blob, from_disk).map(|shader| shader.as_ref().clone())
cached_shader(shader_cache(), key, blob, from_disk).map(|shader| (shader.as_ref().clone(), from_cache))
}

/// Translations on disk: a console program's (DXIL, static samplers) by its bytes, so a run
Expand Down Expand Up @@ -1021,7 +1060,7 @@ pub(crate) fn console_translation(blob: &[u8], stage: Stage, sampler_base: u32,
let t = translate(f.code, &Options { mode: Mode::Compute(cs), wave64: super::detect_wave64(f.code), split_wave64, static_sampler_base: sampler_base, mem_windows: windows, ..Default::default() })?;
if !t.complete() {
let missing: Vec<&String> = t.unsupported.keys().collect();
return Err(format!("incomplete translation, missing: {missing:?}"));
return Err(format!("{INCOMPLETE_TRANSLATION_PREFIX}missing: {missing:?}"));
}
return Ok((t, "cs_6_6"));
}
Expand Down Expand Up @@ -1050,7 +1089,7 @@ pub(crate) fn console_translation(blob: &[u8], stage: Stage, sampler_base: u32,
let t = translate(code, &Options { mode, wave64: super::fssm_wave64(f, code), split_wave64, static_sampler_base: sampler_base, mem_windows: windows, ..Default::default() })?;
if !t.complete() {
let missing: Vec<&String> = t.unsupported.keys().collect();
return Err(format!("incomplete translation, missing: {missing:?}"));
return Err(format!("{INCOMPLETE_TRANSLATION_PREFIX}missing: {missing:?}"));
}
Ok((t, profile))
}
Expand Down Expand Up @@ -1081,7 +1120,7 @@ fn finish_two_pass_compiled(cs: Compiled, record: super::MsInterface, ps: Option
fn finish_two_pass(t: super::Translation, record: super::MsInterface, ps: Option<&[u8]>) -> Result<TwoPass, String> {
if !t.complete() {
let missing: Vec<&String> = t.unsupported.keys().collect();
return Err(format!("incomplete translation, missing: {missing:?}"));
return Err(format!("{INCOMPLETE_TRANSLATION_PREFIX}missing: {missing:?}"));
}
let cs = dxc::compile_cached(&t.hlsl, "main", "cs_6_6")?.0;
finish_two_pass_compiled(Compiled { dispatch_chain: None, dxil: cs, samplers: t.static_samplers }, record, ps)
Expand Down Expand Up @@ -1118,7 +1157,7 @@ fn compile_console_mesh(
let t = super::translate_mesh(code, ms.clone(), wave64, split_wave64, windows)?;
if !t.complete() {
let missing: Vec<&String> = t.unsupported.keys().collect();
return Err(format!("incomplete translation, missing: {missing:?}"));
return Err(format!("{INCOMPLETE_TRANSLATION_PREFIX}missing: {missing:?}"));
}
let dxil = dxc::compile_cached(&t.hlsl, "main", "cs_6_6")?.0;
Ok(Compiled { dispatch_chain: None, dxil, samplers: t.static_samplers })
Expand Down
66 changes: 62 additions & 4 deletions crates/xcl/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,7 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, on
let n = input.len().min(limit);
let next = AtomicUsize::new(0);
let (done, compiled, cached, failed, skipped) = (AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0));
let (ps_base0, ps_static_samplers, vs_static_samplers) = (AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0));
let errors: Mutex<Vec<String>> = Mutex::new(Vec::new());
let matched: Mutex<std::collections::HashSet<u64>> = Mutex::new(Default::default());
let pipeline_jobs: Mutex<Vec<PipelineJob>> = Mutex::new(Vec::new());
Expand Down Expand Up @@ -1884,8 +1885,8 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, on
// a compute program as a title's pipeline translates it (`pso::console_to_dxil_in`):
// into the translation cache too, so the title does not translate it again
if sh.fssm.iter().any(|f| matches!(recomp::interface(&blob, &sh, f), Some(recomp::Mode::Compute(_)))) {
match recomp::pso::console_to_dxil_in(&blob, recomp::pso::Stage::Compute, 0, split, windows) {
Ok(c) => {
match recomp::pso::console_to_dxil_in_status(&blob, recomp::pso::Stage::Compute, 0, split, windows) {
Ok((c, from_cache)) => {
if let Some(p) = pipelines {
if startup.is_some() {
p.make(&c);
Expand All @@ -1897,7 +1898,11 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, on
pipeline_jobs.lock().unwrap().push(PipelineJob { used, compiled: c });
}
}
compiled.fetch_add(1, Ordering::Relaxed)
if from_cache {
cached.fetch_add(1, Ordering::Relaxed)
} else {
compiled.fetch_add(1, Ordering::Relaxed)
}
}
Err(e) => {
let mut g = errors.lock().unwrap();
Expand All @@ -1910,8 +1915,55 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, on
record_done();
continue;
}
let (mut seen_vs, mut seen_ps) = (false, false);
for f in &sh.fssm {
let first_hw_stage = match f.stages.first().map(|s| s.hw) {
Some(4) if !seen_vs => { seen_vs = true; Some(recomp::pso::Stage::Vertex) }
Some(5) if !seen_ps => { seen_ps = true; Some(recomp::pso::Stage::Pixel) }
_ => None,
};
let Some(mode) = recomp::interface(&blob, &sh, f) else { continue };
let mut handled_first_program = false;
let runtime_stage = match (&mode, first_hw_stage) {
(recomp::Mode::Vertex(_), Some(recomp::pso::Stage::Vertex)) => Some(recomp::pso::Stage::Vertex),
(recomp::Mode::Pixel(_), Some(recomp::pso::Stage::Pixel)) => Some(recomp::pso::Stage::Pixel),
_ => None,
};
if let Some(stage) = runtime_stage {
if !f.programs.is_empty() {
handled_first_program = true;
match recomp::pso::console_to_dxil_in_outcome(&blob, stage, 0, graphics_split, windows) {
recomp::pso::CompileOutcome::Compiled { compiled: c, from_cache } => {
if stage == recomp::pso::Stage::Pixel {
ps_base0.fetch_add(1, Ordering::Relaxed);
if !c.samplers.is_empty() {
ps_static_samplers.fetch_add(1, Ordering::Relaxed);
}
} else if !c.samplers.is_empty() {
vs_static_samplers.fetch_add(1, Ordering::Relaxed);
}
if from_cache {
cached.fetch_add(1, Ordering::Relaxed);
} else {
compiled.fetch_add(1, Ordering::Relaxed);
}
}
// Incomplete translation (`!t.complete()`): match the per-program
// skip count on hosted startup without re-translating `programs[0]`.
recomp::pso::CompileOutcome::Incomplete(_) => {
if startup.is_some() { skipped.fetch_add(1, Ordering::Relaxed); }
}
recomp::pso::CompileOutcome::Failed(e) => {
let mut g = errors.lock().unwrap();
if g.len() < 10 {
g.push(format!("{name}: {}", e.lines().find(|l| l.contains("error")).unwrap_or(e.lines().next().unwrap_or(""))));
}
failed.fetch_add(1, Ordering::Relaxed);
}
}
}
}
if handled_first_program && f.programs.len() == 1 { continue; }
let jobs: Vec<(&str, Option<String>)> = match &mode {
recomp::Mode::Vertex(_) => vec![("vs_6_6", None)],
recomp::Mode::Pixel(_) => vec![("ps_6_6", None)],
Expand All @@ -1922,7 +1974,7 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, on
continue;
}
};
for p in &f.programs {
for p in f.programs.iter().skip(usize::from(handled_first_program)) {
let code = &f.data[p.offset..p.offset + p.len];
// as `pso` translates: with the title's memory windows (`console_translation`,
// `translate_mesh`)
Expand Down Expand Up @@ -1981,6 +2033,12 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, on
skipped.into_inner(),
t0.elapsed().as_secs_f64()
);
let (ps_base0, ps_static_samplers, vs_static_samplers) = (ps_base0.into_inner(), ps_static_samplers.into_inner(), vs_static_samplers.into_inner());
if ps_base0 > 0 || vs_static_samplers > 0 {
println!(
"graphics variants (runtime path): PS warmed at assumed sampler_base=0: {ps_base0} ({ps_static_samplers} with static samplers); VS with static samplers shifting paired PS base: {vs_static_samplers}"
);
}
if let Some(p) = pipelines {
if startup.is_none() {
make_pipelines(p, pipeline_jobs.into_inner().unwrap());
Expand Down
Loading
Loading