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
59 changes: 56 additions & 3 deletions crates/xcl/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,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 unused = AtomicUsize::new(0);
if let Some(u) = used {
Expand Down Expand Up @@ -1649,12 +1650,16 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, on
done.fetch_add(1, Ordering::Relaxed);
continue;
}
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 {
p.make(&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 @@ -1670,8 +1675,50 @@ fn precompile(input: &Input, limit: usize, windows: u32, split: Option<bool>, on
}
continue;
}
let (mut warmed_vs, mut warmed_ps) = (false, false);
for f in &sh.fssm {
let Some(mode) = recomp::interface(&blob, &sh, f) else { continue };
let runtime_stage = match (&mode, f.stages.first().map(|s| s.hw)) {
(recomp::Mode::Vertex(_), Some(4)) => Some((recomp::pso::Stage::Vertex, &mut warmed_vs)),
(recomp::Mode::Pixel(_), Some(5)) => Some((recomp::pso::Stage::Pixel, &mut warmed_ps)),
_ => None,
};
if let Some((stage, warmed)) = runtime_stage {
if *warmed || f.programs.is_empty() {
continue;
}
*warmed = 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);
}
continue;
}
// Incomplete translation (`!t.complete()`): fall through to the existing
// per-program loop below so standalone `--precompile` and hosted
// `prepare_for_launch` keep their exact skip behavior without errors.
recomp::pso::CompileOutcome::Incomplete(_) => {}
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);
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 Down Expand Up @@ -1722,6 +1769,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 used.is_some() {
println!("compute programs passed over (no earlier run used their pipeline): {}", unused.into_inner());
}
Expand Down
Loading
Loading