Skip to content
Merged
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
140 changes: 133 additions & 7 deletions crates/victory-gpu/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ pub(crate) struct Shader {
pub(crate) stage: &'static str,
/// The first 8 bytes of its BLAKE3, as logged.
pub(crate) hash: String,
#[cfg(windows)]
full_hash: [u8; 32],
/// The container's length, as logged once its bytes are gone.
size: usize,
/// The container, until `translated` has made its translation (or its
/// refusal) and let it go: a run holds one per distinct shader its title
/// passes, and nothing reads it after. Off Windows nothing translates,
/// so only tests keep it.
/// Released after translation unless an experimental mesh/pixel pair may need it.
#[cfg(any(windows, test))]
container: Mutex<Vec<u8>>,
#[cfg(windows)]
Expand All @@ -88,15 +87,21 @@ fn shader(stage: &'static str, bytes: &[u8]) -> Arc<Shader> {
shaders
.get_or_insert_with(HashMap::new)
.entry(*full.as_bytes())
.or_insert_with(|| Arc::new(Shader::new(stage, bytes)))
.or_insert_with(|| Arc::new(Shader::with_hash(stage, bytes, full)))
.clone()
}

impl Shader {
fn new(stage: &'static str, bytes: &[u8]) -> Shader {
Self::with_hash(stage, bytes, blake3::hash(bytes))
}

fn with_hash(stage: &'static str, bytes: &[u8], digest: blake3::Hash) -> Shader {
Shader {
stage,
hash: blake3::hash(bytes).to_hex()[..16].to_owned(),
hash: digest.to_hex()[..16].to_owned(),
#[cfg(windows)]
full_hash: *digest.as_bytes(),
size: bytes.len(),
#[cfg(any(windows, test))]
container: Mutex::new(bytes.to_vec()),
Expand Down Expand Up @@ -126,7 +131,17 @@ impl Shader {
let done = translate(&container);
// After the translation, not before: if it panics, the
// bytes are still there for the next try.
*container = Vec::new();
let keep = link_mesh_outputs() && done.0.as_ref().is_ok_and(|t| {
self.stage == "pixel"
|| (self.stage == "mesh"
&& victory_shader::emit::mesh_outputs(&t.host.dxil)
.is_some_and(|o| {
o.bytes() > victory_shader::emit::METAL_MESH_OUTPUT_BYTES
}))
});
if !keep {
*container = Vec::new();
}
done
};
let ms = started.elapsed().as_millis() as u64;
Expand Down Expand Up @@ -233,6 +248,117 @@ fn with_resources(
Ok(Translated { host, used })
}

#[cfg(windows)]
fn link_mesh_outputs() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| crate::switch::on("VICTORY_LINK_MESH_OUTPUTS"))
}

#[cfg(windows)]
struct LinkedPair {
mesh: Arc<Shader>,
pixel: Option<Arc<Shader>>,
}

#[cfg(windows)]
type LinkedKey = ([u8; 32], Option<[u8; 32]>);

#[cfg(windows)]
impl Shader {
fn with_translation(&self, translated: Translated) -> Arc<Shader> {
let cell = OnceLock::new();
let _ = cell.set(Some(translated));
Arc::new(Shader {
stage: self.stage,
hash: self.hash.clone(),
full_hash: self.full_hash,
size: self.size,
container: Mutex::new(Vec::new()),
translated: cell,
})
}
}

/// Re-translates a mesh shader together with its paired pixel shader (if any)
/// so arbitrary mesh outputs unread by the pixel shader are pruned under
/// Metal's 32 KiB mesh output limit (`victory_shader::emit::METAL_MESH_OUTPUT_BYTES`).
#[cfg(windows)]
pub(crate) fn translate_linked(
ms: &Shader,
ps: Option<&Shader>,
) -> Option<(Arc<Shader>, Option<Arc<Shader>>)> {
static LINKED: Mutex<Option<HashMap<LinkedKey, Option<LinkedPair>>>> = Mutex::new(None);
let key = (ms.full_hash, ps.map(|p| p.full_hash));
if let Some(hit) = LINKED
.lock()
.unwrap_or_else(|e| e.into_inner())
.as_ref()
.and_then(|m| m.get(&key))
{
return hit
.as_ref()
.map(|pair| (pair.mesh.clone(), pair.pixel.clone()));
}
let mesh_bytes = ms
.container
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone();
let pixel_bytes = ps.map(|p| {
p.container
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()
});
let options = crate::shader_cache::runtime_options();
let result = match victory_shader::emit::translate_linked(
&mesh_bytes,
pixel_bytes.as_deref(),
options,
) {
Ok((ms_host, ps_host)) => {
let ms_bytes = victory_shader::emit::mesh_outputs(&ms_host.dxil).map(|o| o.bytes());
let ms_used = ms.translated()?.used.clone();
let mesh = ms.with_translation(Translated {
host: ms_host,
used: ms_used,
});
let pixel = match (ps, ps_host) {
(Some(p), Some(host)) => Some(p.with_translation(Translated {
host,
used: p.translated()?.used.clone(),
})),
_ => None,
};
tracing::info!(
mesh = %ms.hash,
pixel = ps.map_or("none", |p| p.hash.as_str()),
bytes = ms_bytes,
"mesh and pixel linked under 32 KiB output budget"
);
Some(LinkedPair { mesh, pixel })
}
Err(e) => {
tracing::warn!(
mesh = %ms.hash,
pixel = ps.map_or("none", |p| p.hash.as_str()),
error = %e,
"linked mesh/pixel translation failed; using standalone translation"
);
None
}
};
let out = result
.as_ref()
.map(|pair| (pair.mesh.clone(), pair.pixel.clone()));
LINKED
.lock()
.unwrap_or_else(|e| e.into_inner())
.get_or_insert_with(HashMap::new)
.insert(key, result);
out
}

#[repr(C)]
struct Pipeline {
object: Object,
Expand Down
20 changes: 20 additions & 0 deletions crates/victory-gpu/src/pipeline/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,26 @@ impl HostPipeline {
for (s, _) in &stages {
s.translated()?;
}
// Mesh shaders whose standalone output footprint exceeds Metal's 32 KiB
// limit are re-translated together with their paired pixel shader,
// pruning outputs the pixel shader never reads.
let linked = mesh.filter(|_| super::link_mesh_outputs()).and_then(|ms| {
let bytes = victory_shader::emit::mesh_outputs(&ms.translated()?.host.dxil)?.bytes();
if bytes <= victory_shader::emit::METAL_MESH_OUTPUT_BYTES {
return None;
}
super::translate_linked(ms, find("pixel").map(Arc::as_ref))
});
let stages: Vec<(&Arc<Shader>, D3D12_SHADER_VISIBILITY)> = match &linked {
Some((ms, ps)) => {
let mut v = vec![(ms, D3D12_SHADER_VISIBILITY_MESH)];
if let Some(ps) = ps {
v.push((ps, D3D12_SHADER_VISIBILITY_PIXEL));
}
v
}
None => stages,
};
let host = host()?;
// `SFI0`: 64-bit atomics on typed resources and on heap resources.
let atomics64 = stages.iter().any(|(s, _)| {
Expand Down
14 changes: 9 additions & 5 deletions crates/victory-gpu/src/shader_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,23 @@ fn options_build(locks: bool, bias: bool, narrow: bool) -> &'static [u8; 32] {
})
}

/// The translation of a console shader container, from the cache when
/// there is one, and whether it came from there.
pub fn host_shader(container: &[u8]) -> (Result<HostShader, String>, bool) {
pub(crate) fn runtime_options() -> victory_shader::emit::Options {
// Writes through views mark their pages: copy-backs compare only those
// (`crate::shadow::copy_back_marks`).
// Under Wine (D3DMetal), 64-bit image atomics run under locks: its own
// leave the texels untouched (`emit`, "Atomic locks").
let options = victory_shader::emit::Options {
victory_shader::emit::Options {
view_marks: true,
atomic_locks: atomic_locks(),
bias_as_gradients: bias_as_gradients(),
narrow_waves: narrow_waves(),
};
}
}

/// The translation of a console shader container, from the cache when
/// there is one, and whether it came from there.
pub fn host_shader(container: &[u8]) -> (Result<HostShader, String>, bool) {
let options = runtime_options();
let translate =
|c: &[u8]| victory_shader::emit::translate_with(c, options).map_err(|e| e.to_string());
let build = if options.atomic_locks || options.bias_as_gradients || options.narrow_waves {
Expand Down
Loading