Faces move, ray tracing mostly off, intro video fix, picture fixes
- Faces: the face network's 16-bit values travel through integer views; they were read as floats and came back as zeros on NVIDIA. Port of fxceless/victory-fork pull request 927 (tippythetaidum): emit/halves.rs, lower.rs, descriptor/host.rs, draw.rs. - Ray tracing mostly off: scripts/rt_off_hashes.txt lists the 1,000 CS_RaytracingResolve and 34 traced-shadow shaders, skipped by play-nvidia.ps1 through VICTORY_SKIP_PIPELINES (-RayTracing keeps them). The structure builder cannot run on NVIDIA, so every ray missed and rooms went white depending on the view angle. The screen-probe and visibility traces keep running; skipping them left rainbow glitter. - Intro video flicker: linear one-level R8 video planes always record their copy (texture.rs), so frames of the ring no longer show out of order. - VICTORY_AMBIENT_MOST (ambient.rs), VICTORY_NAN_SCRUB (emit/lower.rs) and VICTORY_PIPELINE_WAIT_ONCE (pipeline/make.rs, on by default). - Off by default: VICTORY_CONSTANT_MARKS (port of fxceless pull request 1216), VICTORY_TARGET_VARIANTS (port of fxceless a35c557393), F9 frame capture (capture.rs). - play-nvidia.ps1: the speed set by default, -Quality for the slower set with fewer stretched bodies and less rainbow skin. README updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
3e5fb36392
commit
77e13a974a
27 files changed
+1868
-73
No files matched your search
@@ -1237,7 +1237,24 @@ fn translated_compute_matches_pc() {
|
||||
};
|
||||
roots.push((1 + n as u32, Root::View { srv, data }));
|
||||
}
|
||||
let ours = host.dispatch(&root_t, &pso_t, &dispatch(roots));
|
||||
// A buffer of halves the translated program moves as unsigned
|
||||
// integers is shown to it as its unsigned twin, as the host does
|
||||
// (`HostShader::half_views`); the PC program keeps the float view.
|
||||
let twins: Vec<View> = views
|
||||
.iter()
|
||||
.map(|&v| match v {
|
||||
View::HalfUav(n) if !cs_c.half_views.is_empty() => View::HalfUintUav(n),
|
||||
v => v,
|
||||
})
|
||||
.collect();
|
||||
let ours = host.dispatch(
|
||||
&root_t,
|
||||
&pso_t,
|
||||
&Dispatch {
|
||||
views: &twins,
|
||||
..dispatch(roots)
|
||||
},
|
||||
);
|
||||
if name.starts_with("terminates") {
|
||||
match ours {
|
||||
Ok(_) => eprintln!("{name}: finished"),
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
//! Typed buffer loads that move 16-bit halves without the d16 form: a
|
||||
//! format load of whole dwords whose result the program reads only one
|
||||
//! half of, as a half float, moves 16-bit integers, like a d16 access
|
||||
//! ([`super::super::HostShader::half_views`]).
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use super::{Fields, Inst, Lower, Operand, Reg, Role, RootType, Source, Value};
|
||||
|
||||
impl<'a> Lower<'a> {
|
||||
/// Whether every use of `value`, a typed buffer load's result, reads
|
||||
/// one 16-bit half of it as a half float: a `v_fma_mix*` source with
|
||||
/// its `op_sel_hi` bit set, or `v_cvt_f32_f16`. A program that takes a
|
||||
/// format load apart like that reads a 16-bit integer view on the
|
||||
/// console (the load gives the zero-extended integer, whose bits are a
|
||||
/// half; a float view's value it would not take apart). A use is a
|
||||
/// read as an operand ([`crate::ir::operand_reads`]): a later write of
|
||||
/// the register reads the value only to keep it in the lanes EXEC
|
||||
/// leaves alone, and a phi carries it on, its own uses counting. False
|
||||
/// without a use.
|
||||
pub(super) fn read_only_as_halves(&self, value: Value) -> bool {
|
||||
let p = self.x.p;
|
||||
let mut used = false;
|
||||
let mut seen = HashSet::from([value]);
|
||||
let mut todo = vec![value];
|
||||
while let Some(value) = todo.pop() {
|
||||
for b in &p.blocks {
|
||||
for phi in b.phis.iter().filter(|phi| phi.args.contains(&value)) {
|
||||
if seen.insert(phi.value) {
|
||||
todo.push(phi.value);
|
||||
}
|
||||
}
|
||||
for i in &b.insts {
|
||||
if !i.reads.iter().any(|r| r.1 == value) {
|
||||
continue;
|
||||
}
|
||||
let operands = crate::ir::operand_reads(p, i);
|
||||
let regs: Vec<Reg> = i
|
||||
.reads
|
||||
.iter()
|
||||
.filter(|r| r.1 == value && operands.contains(&r.0))
|
||||
.map(|r| r.0)
|
||||
.collect();
|
||||
if regs.is_empty() {
|
||||
continue;
|
||||
}
|
||||
used = true;
|
||||
let Fields::Valu(v) = &i.fields else {
|
||||
return false;
|
||||
};
|
||||
match i.name() {
|
||||
"v_cvt_f32_f16" => {}
|
||||
"v_fma_mix_f32" | "v_fma_mixlo_f16" | "v_fma_mixhi_f16" => {
|
||||
let whole = v.src.iter().enumerate().any(|(k, op)| {
|
||||
matches!(*op, Operand::Vector { reg, .. } if regs.contains(&Reg::V(reg)))
|
||||
&& v.opsel_hi >> k & 1 == 0
|
||||
});
|
||||
if whole {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
_ => return false,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
used
|
||||
}
|
||||
|
||||
/// Whether `inst` is a format load of whole dwords (not d16) whose
|
||||
/// every channel is read only as halves ([`Self::read_only_as_halves`]).
|
||||
pub(super) fn format_load_read_as_halves(&self, inst: &Inst) -> bool {
|
||||
let Fields::Buffer {
|
||||
data: Some(Operand::Vector { reg, .. }),
|
||||
..
|
||||
} = inst.fields
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
let name = inst.name();
|
||||
let count = match name
|
||||
.strip_prefix("buffer_load_format_")
|
||||
.or(name.strip_prefix("tbuffer_load_format_"))
|
||||
{
|
||||
Some("x") => 1,
|
||||
Some("xy") => 2,
|
||||
Some("xyz") => 3,
|
||||
Some("xyzw") => 4,
|
||||
_ => return false,
|
||||
};
|
||||
(0..count).all(|k| {
|
||||
inst.write(Reg::V(reg + k))
|
||||
.is_some_and(|v| self.read_only_as_halves(v))
|
||||
})
|
||||
}
|
||||
|
||||
/// The table places ([`Self::table_place`]) every access through which
|
||||
/// moves halves: a d16 access, or a format load read only as halves
|
||||
/// ([`Self::format_load_read_as_halves`]). A place the program reaches
|
||||
/// otherwise too is left out (a format load of numbers beside one of
|
||||
/// halves, a raw or scalar load, a descriptor chosen by control flow),
|
||||
/// and so is every place of a table the program also picks from at run
|
||||
/// time: what the program makes of such a view is not halves alone.
|
||||
pub(super) fn places_read_as_halves(&self) -> std::collections::BTreeSet<(u8, u32)> {
|
||||
let mut halves = std::collections::BTreeSet::new();
|
||||
let mut other = std::collections::BTreeSet::new();
|
||||
let mut picked = HashSet::new();
|
||||
for d in &self.x.res.descriptors {
|
||||
if d.role == Role::Sampler {
|
||||
continue;
|
||||
}
|
||||
let inst = self
|
||||
.x
|
||||
.p
|
||||
.blocks
|
||||
.get(d.site.0)
|
||||
.and_then(|b| b.insts.get(d.site.1));
|
||||
let half = matches!(d.source, Source::Loaded { .. })
|
||||
&& inst.is_some_and(|i| {
|
||||
(i.name().starts_with("buffer_") && i.name().contains("_format_d16_"))
|
||||
|| self.format_load_read_as_halves(i)
|
||||
});
|
||||
for leaf in d.source.leaves() {
|
||||
let Source::Loaded { load, .. } = leaf else {
|
||||
continue;
|
||||
};
|
||||
let Some(a) = self.scalar.get(load) else {
|
||||
continue;
|
||||
};
|
||||
if a.dynamic.is_some() {
|
||||
if let Some(rp) = self.x.native.layout.get(a.param)
|
||||
&& rp.root == RootType::Table
|
||||
{
|
||||
picked.insert(rp.slot);
|
||||
}
|
||||
} else if let Some(place) = self.table_place(leaf) {
|
||||
if half {
|
||||
halves.insert(place);
|
||||
} else {
|
||||
other.insert(place);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
halves.retain(|p| !other.contains(p) && !picked.contains(&p.0));
|
||||
halves
|
||||
}
|
||||
}
|
||||
@@ -612,6 +612,7 @@ fn stub(pixel: bool, outputs: Vec<SigElement>) -> HostShader {
|
||||
root_srvs: Vec::new(),
|
||||
root_uavs: Vec::new(),
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -991,6 +992,7 @@ fn transition(
|
||||
|
||||
/// A view in the heap for [`Host::dispatch`].
|
||||
#[cfg(test)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum View<'a> {
|
||||
/// An RGBA32F texture SRV (width, height, texels).
|
||||
Texture(u32, u32, &'a [[f32; 4]]),
|
||||
@@ -1004,6 +1006,9 @@ pub enum View<'a> {
|
||||
UintTextureUav(u32, u32),
|
||||
/// A typed RGBA16F buffer UAV of this many elements, zeroed.
|
||||
HalfUav(u32),
|
||||
/// The same buffer viewed as R16G16B16A16_UINT: what a translated
|
||||
/// program that moves halves is shown ([`super::HostShader::half_views`]).
|
||||
HalfUintUav(u32),
|
||||
/// A constant buffer of these dwords.
|
||||
Cbv(&'a [u32]),
|
||||
/// A buffer SRV over these dwords.
|
||||
@@ -1240,7 +1245,7 @@ impl Host {
|
||||
);
|
||||
keep.push(tex);
|
||||
}
|
||||
View::RawUav(_) | View::HalfUav(_) => {
|
||||
View::RawUav(_) | View::HalfUav(_) | View::HalfUintUav(_) => {
|
||||
// (dwords, format, elements, flags)
|
||||
let (n, format, elements, flags) = match *v {
|
||||
View::HalfUav(e) => (
|
||||
@@ -1249,6 +1254,12 @@ impl Host {
|
||||
e,
|
||||
D3D12_BUFFER_UAV_FLAG_NONE,
|
||||
),
|
||||
View::HalfUintUav(e) => (
|
||||
2 * e,
|
||||
DXGI_FORMAT_R16G16B16A16_UINT,
|
||||
e,
|
||||
D3D12_BUFFER_UAV_FLAG_NONE,
|
||||
),
|
||||
View::RawUav(n) => {
|
||||
(n, DXGI_FORMAT_R32_TYPELESS, n, D3D12_BUFFER_UAV_FLAG_RAW)
|
||||
}
|
||||
|
||||
@@ -194,6 +194,7 @@ pub(super) fn build_compute_shader(
|
||||
root_srvs: lowered.root_srvs,
|
||||
root_uavs: lowered.root_uavs,
|
||||
root_indexed_views: res.root_indexed_views.clone(),
|
||||
half_views: lowered.half_views,
|
||||
};
|
||||
if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
|
||||
let mut copy = shader.dxil.clone();
|
||||
|
||||
@@ -25,6 +25,11 @@ use crate::structure::{Atom, Bool, LaneExit, Structured};
|
||||
#[path = "intersect.rs"]
|
||||
pub(super) mod intersect;
|
||||
|
||||
/// Format loads that move 16-bit halves without the d16 form, a child
|
||||
/// module so it shares the emitter's state.
|
||||
#[path = "halves.rs"]
|
||||
mod halves;
|
||||
|
||||
type Why = &'static str;
|
||||
|
||||
/// How an interpolation evaluates an input channel.
|
||||
@@ -55,6 +60,8 @@ pub(crate) struct Lowered {
|
||||
pub root_cbvs: Vec<u8>,
|
||||
pub root_srvs: Vec<u8>,
|
||||
pub root_uavs: Vec<u8>,
|
||||
/// See [`super::HostShader::half_views`].
|
||||
pub half_views: Vec<(u8, u32)>,
|
||||
pub user_data: Vec<UserData>,
|
||||
pub samplers: Vec<[u32; 4]>,
|
||||
/// The wave size the shader requires of the host, if any.
|
||||
@@ -96,6 +103,12 @@ pub(crate) fn lower(parts: &Parts) -> Result<Lowered, ShaderError> {
|
||||
/// and store). Not where a group barrier would wait for lanes that left, nor
|
||||
/// where two host waves of 32 stand for a console wave (they exchange, which
|
||||
/// takes all lanes).
|
||||
/// LOCAL: `VICTORY_NAN_SCRUB=1` (off by default): see `Lower::scrub_nan`.
|
||||
pub(crate) fn nan_scrub() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var_os("VICTORY_NAN_SCRUB").is_some_and(|v| v == "1"))
|
||||
}
|
||||
|
||||
fn counted_in_threads(parts: &Parts) -> bool {
|
||||
let crate::native::StageIo::Cs(cs) = &parts.native.io else {
|
||||
return false;
|
||||
@@ -295,6 +308,11 @@ struct Lower<'a> {
|
||||
params: HashMap<Value, (usize, u32)>,
|
||||
user_data: Option<V>,
|
||||
user_reads: BTreeMap<u32, UserData>,
|
||||
/// Table descriptors read or written sixteen bits a channel
|
||||
/// ([`super::HostShader::half_views`]): root parameter, dword offset.
|
||||
half_views: std::collections::BTreeSet<(u8, u32)>,
|
||||
/// [`Self::places_read_as_halves`], found before the code is lowered.
|
||||
halves_only: std::collections::BTreeSet<(u8, u32)>,
|
||||
samplers: Vec<[u32; 4]>,
|
||||
descriptors: HashMap<(Site, bool), &'a Source>,
|
||||
scalar: HashMap<Site, Address>,
|
||||
@@ -1635,6 +1653,8 @@ impl<'a> Lower<'a> {
|
||||
params: x.res.params.iter().map(|&(v, p, d)| (v, (p, d))).collect(),
|
||||
user_data: None,
|
||||
user_reads: BTreeMap::new(),
|
||||
half_views: Default::default(),
|
||||
halves_only: Default::default(),
|
||||
samplers: Vec::new(),
|
||||
descriptors: x
|
||||
.res
|
||||
@@ -2335,6 +2355,7 @@ impl<'a> Lower<'a> {
|
||||
self.s.entry = body;
|
||||
}
|
||||
self.find_coherent();
|
||||
self.halves_only = self.places_read_as_halves();
|
||||
let order = crate::ir::reverse_postorder(&p.blocks);
|
||||
for (n, &b) in order.iter().enumerate() {
|
||||
self.rpo[b] = n;
|
||||
@@ -2420,6 +2441,7 @@ impl<'a> Lower<'a> {
|
||||
let root_cbvs = self.s.root_cbvs.iter().map(|r| r.0).collect();
|
||||
let root_srvs = self.s.root_srvs.iter().map(|r| r.0).collect();
|
||||
let root_uavs = self.s.root_uavs.iter().map(|r| r.0).collect();
|
||||
let half_views = std::mem::take(&mut self.half_views).into_iter().collect();
|
||||
let cmp_samplers = self.s.cmp_samplers;
|
||||
let wave_size = self.s.wave_size;
|
||||
Ok(Lowered {
|
||||
@@ -2428,6 +2450,7 @@ impl<'a> Lower<'a> {
|
||||
root_cbvs,
|
||||
root_srvs,
|
||||
root_uavs,
|
||||
half_views,
|
||||
#[cfg(test)]
|
||||
nonuniform_handles: self.s.nonuniform_handles,
|
||||
#[cfg(test)]
|
||||
@@ -6197,7 +6220,12 @@ impl<'a> Lower<'a> {
|
||||
raw.push((x, zero));
|
||||
}
|
||||
let raw = self.end_guarded(at, &raw);
|
||||
let other = self.s.m.cmp(self.cur, pred::EQ, stride, zero);
|
||||
// LOCAL (fxceless #1216): only a slot marked as a constant
|
||||
// buffer is read as one; any other non-buffer gives zeros.
|
||||
let other = match self.slot_is_constant(src)? {
|
||||
Some(is_constant) => is_constant,
|
||||
None => self.s.m.cmp(self.cur, pred::EQ, stride, zero),
|
||||
};
|
||||
let at = self.begin_guarded(Some(other));
|
||||
let h = self.handle(site, src, false, (13, 0x1_0000))?;
|
||||
let mut both = Vec::new();
|
||||
@@ -6482,7 +6510,15 @@ impl<'a> Lower<'a> {
|
||||
let props = if un.starts_with("buffer_load_format")
|
||||
|| un.starts_with("buffer_store_format")
|
||||
{
|
||||
(10 | u, 0x409)
|
||||
// (Unsigned integers where the access moves halves.)
|
||||
let halves = self
|
||||
.table_place(&Source::Loaded { load: site, first })
|
||||
.is_some_and(|p| {
|
||||
self.halves_only.contains(&p)
|
||||
&& (un.contains("_format_d16_")
|
||||
|| self.format_load_read_as_halves(ui))
|
||||
});
|
||||
(10 | u, if halves { 0x405 } else { 0x409 })
|
||||
} else if !counted
|
||||
&& *idxen
|
||||
&& format.is_none()
|
||||
@@ -7122,6 +7158,22 @@ impl<'a> Lower<'a> {
|
||||
(kind == Some(ResourceType::Uav)).then(|| self.host_word(RootType::Other(USER_VIEW_BIAS), rp.slot, 0))
|
||||
}
|
||||
|
||||
/// The table descriptor `src` is, as (root parameter, dword in the
|
||||
/// table), when the code loads it from a fixed place in a table: the
|
||||
/// load's own place, not the start of the range its used-resource
|
||||
/// entry describes (a range of several descriptors starts before most
|
||||
/// of them). `None` for a descriptor picked at run time, built in the
|
||||
/// code or chosen by control flow.
|
||||
fn table_place(&self, src: &Source) -> Option<(u8, u32)> {
|
||||
let Source::Loaded { load, first } = src else {
|
||||
return None;
|
||||
};
|
||||
let a = self.scalar.get(load)?;
|
||||
let rp = self.x.native.layout.get(a.param)?;
|
||||
(rp.root == RootType::Table && a.dynamic.is_none())
|
||||
.then(|| (rp.slot, a.offset / 4 + u32::from(*first)))
|
||||
}
|
||||
|
||||
/// The used-resource entry a descriptor loaded from a table falls in.
|
||||
fn used_kind(&self, src: &Source) -> Option<&'a UsedResource> {
|
||||
if let Source::Phi { args, .. } | Source::Select { args, .. } | Source::Lanes { args, .. } =
|
||||
@@ -7477,10 +7529,33 @@ impl<'a> Lower<'a> {
|
||||
let word = self.slot_word(src)?;
|
||||
Ok(word.map(|word| {
|
||||
let bits = self.s.m.i32(super::shader::VIEW_STRIDE_BITS);
|
||||
self.s.m.bin(self.cur, Bin::And, word, bits)
|
||||
let stride = self.s.m.bin(self.cur, Bin::And, word, bits);
|
||||
if super::shader::constant_marks() {
|
||||
// LOCAL (fxceless #1216): the mark of a constant buffer
|
||||
// slot is no stride.
|
||||
let mark = self.s.m.i32(super::shader::SLOT_STRIDE_CONSTANT);
|
||||
let is_constant = self.s.m.cmp(self.cur, pred::EQ, word, mark);
|
||||
let zero = self.s.m.i32(0);
|
||||
self.s.m.select(self.cur, is_constant, zero, stride)
|
||||
} else {
|
||||
stride
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
/// LOCAL (fxceless #1216): with [`super::shader::constant_marks`],
|
||||
/// whether the slot `src` names holds a constant buffer view.
|
||||
fn slot_is_constant(&mut self, src: &Source) -> Result<Option<V>, Why> {
|
||||
if !super::shader::constant_marks() {
|
||||
return Ok(None);
|
||||
}
|
||||
let Some(word) = self.slot_word(src)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let mark = self.s.m.i32(super::shader::SLOT_STRIDE_CONSTANT);
|
||||
Ok(Some(self.s.m.cmp(self.cur, pred::EQ, word, mark)))
|
||||
}
|
||||
|
||||
/// Whether the typed buffer view `src` names is one of an integer format,
|
||||
/// and whether a signed one, as [`Self::slot_stride`] (`None`: a view the
|
||||
/// program does not pick from the heap).
|
||||
@@ -10932,6 +11007,10 @@ impl<'a> Lower<'a> {
|
||||
self.float_of(v)?
|
||||
});
|
||||
}
|
||||
// LOCAL: `VICTORY_NAN_SCRUB=1` (see `scrub_nan`).
|
||||
for v in &mut vals {
|
||||
*v = self.scrub_nan(*v);
|
||||
}
|
||||
let h = self.handle(site, src, false, props)?;
|
||||
let at = self.begin_guarded(guard);
|
||||
let mut args = vec![h];
|
||||
@@ -11630,6 +11709,23 @@ impl<'a> Lower<'a> {
|
||||
.copied()
|
||||
.ok_or("an untracked buffer")?;
|
||||
let used = self.used_kind(src);
|
||||
// Sixteen bits a channel through a view of a table: the bits go as
|
||||
// they are, as unsigned integers, and the host is told, which shows
|
||||
// the shader a 16-bit float view there as its unsigned twin
|
||||
// ([`super::HostShader::half_views`]).
|
||||
// A format load of whole dwords whose every use reads one half of a
|
||||
// result as a half float moves 16-bit integers too (the head
|
||||
// program's reads of the face network's output), where every
|
||||
// access through its view does ([`Self::places_read_as_halves`]).
|
||||
// A d16 access too only where every access does: a whole float
|
||||
// read through the same place would see the unsigned twin.
|
||||
let place = typed.then(|| self.table_place(src)).flatten().filter(|p| {
|
||||
self.halves_only.contains(p) && (d16 || self.format_load_read_as_halves(inst))
|
||||
});
|
||||
let halves = place.is_some();
|
||||
if let Some(place) = place {
|
||||
self.half_views.insert(place);
|
||||
}
|
||||
let (base, extra) = self.built_offset(src)?;
|
||||
if base != 0 {
|
||||
let b = self.s.m.i32(base);
|
||||
@@ -11861,14 +11957,16 @@ impl<'a> Lower<'a> {
|
||||
let mut vals =
|
||||
self.vgprs(inst, data.as_ref().ok_or("buffer store data")?, regs as u8)?;
|
||||
if d16 {
|
||||
// d16 data is taken as f16, widened for the 32-bit float
|
||||
// store; through a view of an integer format it is a 16-bit
|
||||
// Through a view of a table each half is stored as it is
|
||||
// (`halves`, fxceless #927). Reached another way, d16 data
|
||||
// is taken as f16, widened for the 32-bit float store;
|
||||
// through a view of an integer format it is a 16-bit
|
||||
// integer, zero- or sign-extended (VIctory 4243a96a: the face
|
||||
// network keeps its halves in UINT views; its output was
|
||||
// noise). shortcut: that is known of the views a program
|
||||
// picks from the heap, not of those in its tables; and the
|
||||
// handle stays a float one, whose bits the drivers pass.
|
||||
let integer = if typed { self.integer_view(src)? } else { None };
|
||||
// picks from the heap; and the handle stays a float one,
|
||||
// whose bits the drivers pass.
|
||||
let integer = if typed && !halves { self.integer_view(src)? } else { None };
|
||||
let mut wide = Vec::new();
|
||||
for k in 0..count {
|
||||
let mut x = vals[(k / 2) as usize];
|
||||
@@ -11876,6 +11974,11 @@ impl<'a> Lower<'a> {
|
||||
let k16 = self.s.m.i32(16);
|
||||
x = self.s.m.bin(c, Bin::LShr, x, k16);
|
||||
}
|
||||
if halves {
|
||||
let low = self.s.m.i32(0xffff);
|
||||
wide.push(self.s.m.bin(c, Bin::And, x, low));
|
||||
continue;
|
||||
}
|
||||
let f32t = self.s.t.f32;
|
||||
let f = self
|
||||
.s
|
||||
@@ -11900,12 +12003,21 @@ impl<'a> Lower<'a> {
|
||||
if count != 4 || *offen || *offset != 0 {
|
||||
return Err("a typed buffer store of some channels or at an offset");
|
||||
}
|
||||
args.push(self.handle(site, src, false, (10 | u, 0x409))?);
|
||||
// Four unsigned integers for halves, four floats otherwise.
|
||||
let component = if halves { 0x405 } else { 0x409 };
|
||||
args.push(self.handle(site, src, false, (10 | u, component))?);
|
||||
args.extend([index.unwrap_or(self.s.m.i32(0)), iu]);
|
||||
for x in vals {
|
||||
args.push(self.as_float(x));
|
||||
args.push(if halves { x } else { self.as_float(x) });
|
||||
}
|
||||
(69, "bufferStore.f32")
|
||||
(
|
||||
69,
|
||||
if halves {
|
||||
"bufferStore.i32"
|
||||
} else {
|
||||
"bufferStore.f32"
|
||||
},
|
||||
)
|
||||
} else {
|
||||
match index {
|
||||
// A raw buffer (stride 1) written by index: the index is the byte.
|
||||
@@ -11965,16 +12077,30 @@ impl<'a> Lower<'a> {
|
||||
if uav {
|
||||
self.s.flags |= flags::TYPED_UAV_LOADS;
|
||||
}
|
||||
let h = self.handle(site, src, false, (10 | u, 0x409))?;
|
||||
let index = index.unwrap_or(self.s.m.i32(0));
|
||||
self.s.op(
|
||||
c,
|
||||
"bufferLoad.f32",
|
||||
Attr::ReadOnly,
|
||||
self.s.t.res_ret,
|
||||
68,
|
||||
&[h, index, iu],
|
||||
)
|
||||
if halves {
|
||||
let h = self.handle(site, src, false, (10 | u, 0x405))?;
|
||||
let i32t = self.s.t.i32;
|
||||
let ret = self.s.m.named_struct("dx.types.ResRet.i32", vec![i32t; 5]);
|
||||
self.s.op(
|
||||
c,
|
||||
"bufferLoad.i32",
|
||||
Attr::ReadOnly,
|
||||
ret,
|
||||
68,
|
||||
&[h, index, iu],
|
||||
)
|
||||
} else {
|
||||
let h = self.handle(site, src, false, (10 | u, 0x409))?;
|
||||
self.s.op(
|
||||
c,
|
||||
"bufferLoad.f32",
|
||||
Attr::ReadOnly,
|
||||
self.s.t.res_ret,
|
||||
68,
|
||||
&[h, index, iu],
|
||||
)
|
||||
}
|
||||
} else {
|
||||
let mask = self.s.m.i8((1u8 << count) - 1);
|
||||
let align = self.s.m.i32(4);
|
||||
@@ -12024,24 +12150,32 @@ impl<'a> Lower<'a> {
|
||||
return Err("a buffer result");
|
||||
};
|
||||
if d16 {
|
||||
// Two f16 channels per VGPR (an odd last high half 0); the low
|
||||
// 16 bits of an integer view's (see the stores above).
|
||||
let integer = if typed { self.integer_view(src)? } else { None };
|
||||
// Two f16 channels per VGPR (an odd last high half 0); through
|
||||
// a view of a table, the 16 bits of each as they are (fxceless
|
||||
// #927); the low 16 bits of an integer view's picked from the
|
||||
// heap (see the stores above).
|
||||
let integer = if typed && !halves { self.integer_view(src)? } else { None };
|
||||
let k16 = self.s.m.i32(16);
|
||||
let i32t = self.s.t.i32;
|
||||
for k in 0..count.div_ceil(2) {
|
||||
let mut x = None;
|
||||
for ch in (2 * k..(2 * k + 2).min(count)).rev() {
|
||||
let f = self.s.m.extract(c, r, ch);
|
||||
let mut h = self
|
||||
.s
|
||||
.op(c, "legacyF32ToF16", Attr::ReadNone, i32t, 130, &[f]);
|
||||
if let Some((int, _)) = integer {
|
||||
let bits = self.as_int(f);
|
||||
let low = self.s.m.i32(0xFFFF);
|
||||
let low = self.s.m.bin(c, Bin::And, bits, low);
|
||||
h = self.s.m.select(c, int, low, h);
|
||||
}
|
||||
let h = if halves {
|
||||
let low = self.s.m.i32(0xffff);
|
||||
self.s.m.bin(c, Bin::And, f, low)
|
||||
} else {
|
||||
let mut h = self
|
||||
.s
|
||||
.op(c, "legacyF32ToF16", Attr::ReadNone, i32t, 130, &[f]);
|
||||
if let Some((int, _)) = integer {
|
||||
let bits = self.as_int(f);
|
||||
let low = self.s.m.i32(0xFFFF);
|
||||
let low = self.s.m.bin(c, Bin::And, bits, low);
|
||||
h = self.s.m.select(c, int, low, h);
|
||||
}
|
||||
h
|
||||
};
|
||||
x = Some(match x {
|
||||
Some(hi) => {
|
||||
let hi = self.s.m.bin(c, Bin::Shl, hi, k16);
|
||||
@@ -12056,12 +12190,34 @@ impl<'a> Lower<'a> {
|
||||
}
|
||||
for k in 0..count {
|
||||
let x = self.s.m.extract(c, r, k);
|
||||
let x = self.as_int(x);
|
||||
// (Integers already where the load moved halves.)
|
||||
let x = if halves { x } else { self.as_int(x) };
|
||||
self.write(inst, Reg::V(reg + k as u8), x, guard);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// LOCAL: with `VICTORY_NAN_SCRUB=1` (off by default), `f` or 0 where it
|
||||
/// is NaN. Used on the colours pixel shaders write and on float image
|
||||
/// stores: a NaN that reaches the title's bloom, blur or temporal
|
||||
/// upscaler spreads into black patches that stay in its history (seen on
|
||||
/// NVIDIA as black blobs on faces and in the air). Not what the console
|
||||
/// does, so it is a switch; part of the shader cache key.
|
||||
fn scrub_nan(&mut self, f: V) -> V {
|
||||
if !nan_scrub() {
|
||||
return f;
|
||||
}
|
||||
// NaN or an infinity (11/26: the whole picture went white at one
|
||||
// angle with only NaN scrubbed: an infinity through bloom or the
|
||||
// exposure does the same). |f| >= inf, unordered: true for NaN too.
|
||||
let c = self.cur;
|
||||
let a = self.unary(6, f);
|
||||
let inf = self.s.m.f32(0x7f80_0000);
|
||||
let bad = self.s.m.cmp(c, pred::UGE, a, inf);
|
||||
let zero = self.s.m.f32(0);
|
||||
self.s.m.select(c, bad, zero, f)
|
||||
}
|
||||
|
||||
fn export(&mut self, b: usize, i: usize) -> Result<(), Why> {
|
||||
let e = *self
|
||||
.exports
|
||||
@@ -12247,6 +12403,8 @@ impl<'a> Lower<'a> {
|
||||
(true, false) => ("storeVertexOutput", 171),
|
||||
(true, true) => ("storePrimitiveOutput", 172),
|
||||
};
|
||||
// LOCAL: a colour a pixel shader writes (`VICTORY_NAN_SCRUB`).
|
||||
let colour = self.s.pixel && matches!(e.target, Target::Color(_));
|
||||
for (id, col, value, half) in stores {
|
||||
let mut x = self.i32_of(value)?;
|
||||
if let Some(high) = half {
|
||||
@@ -12265,12 +12423,19 @@ impl<'a> Lower<'a> {
|
||||
let el = &mut elements(self, primitive)[id];
|
||||
el.usage |= 1 << col;
|
||||
let (suffix, x) = match el.comp {
|
||||
9 if half.is_some() => (".f32", self.as_float(x)),
|
||||
9 if half.is_some() => {
|
||||
let f = self.as_float(x);
|
||||
(".f32", if colour { self.scrub_nan(f) } else { f })
|
||||
}
|
||||
9 => {
|
||||
let f = match self.floats.get(&value) {
|
||||
Some(&f) => f,
|
||||
None => self.as_float(x),
|
||||
};
|
||||
// LOCAL: `VICTORY_NAN_SCRUB=1`: a colour a pixel shader
|
||||
// writes is 0 where it is NaN, so one bad value does not
|
||||
// spread through the title's blur and temporal passes.
|
||||
let f = if colour { self.scrub_nan(f) } else { f };
|
||||
(".f32", f)
|
||||
}
|
||||
_ => (".i32", x),
|
||||
|
||||
@@ -122,8 +122,8 @@ pub use lower::{host_settings, set_host_max_wave, set_host_rdna};
|
||||
pub use rect::rect_list_gs;
|
||||
pub use shader::{
|
||||
MEMORY_FAULTS, MEMORY_PAGES, MEMORY_READ_ONLY, ROOT_CBV_SPACE, SAMPLER_SPACE, SLOT_FAULTS, SLOT_MEMORY, SLOT_STRIDES,
|
||||
SLOT_STRIDES_COUNT, USER_DATA_SPACE, VIEW_INTEGER, VIEW_SIGNED, VIEW_STRIDE_BITS, USER_DISPATCH_THREADS_X, USER_MEMORY_BASE, USER_ROOT_ADDRESS, USER_VIEW_ADDRESS, USER_VIEW_BIAS,
|
||||
raw_views, set_raw_views, set_view_heap_limit, view_heap_limit,
|
||||
SLOT_STRIDE_CONSTANT, SLOT_STRIDES_COUNT, USER_DATA_SPACE, VIEW_INTEGER, VIEW_SIGNED, VIEW_STRIDE_BITS, USER_DISPATCH_THREADS_X, USER_MEMORY_BASE, USER_ROOT_ADDRESS, USER_VIEW_ADDRESS, USER_VIEW_BIAS,
|
||||
constant_marks, raw_views, set_raw_views, set_view_heap_limit, view_heap_limit,
|
||||
};
|
||||
|
||||
/// The loop limit without an override: 2^20.
|
||||
@@ -354,6 +354,21 @@ pub struct HostShader {
|
||||
pub root_uavs: Vec<u8>,
|
||||
/// Heap descriptors that can be selected from root state at bind time.
|
||||
pub root_indexed_views: Vec<crate::resource::RootIndexedView>,
|
||||
/// Table descriptors (root parameter, dword offset in the table) whose
|
||||
/// buffer the shader reads or writes sixteen bits a channel
|
||||
/// (`buffer_load_format_d16_*`, `buffer_store_format_d16_*`), in
|
||||
/// order. On the console each channel's 16 bits move as they are
|
||||
/// through a 16-bit float view and through a 16-bit integer one alike
|
||||
/// (the title keeps half floats, and indices beside them, in
|
||||
/// `R16G16B16A16_UINT` and `R16_UINT` buffers). The DXIL moves them as
|
||||
/// unsigned integers, which an unsigned 16-bit view carries unchanged;
|
||||
/// the host shows the shader a 16-bit float or signed buffer view at
|
||||
/// these places as the unsigned format of the same layout.
|
||||
/// shortcut: a view in a normalised 16-bit format, whose values the
|
||||
/// console would convert to halves, is read as its integers; a
|
||||
/// descriptor picked at run time keeps the float path (its slot, and so
|
||||
/// its format, is known only then).
|
||||
pub half_views: Vec<(u8, u32)>,
|
||||
}
|
||||
|
||||
impl HostShader {
|
||||
@@ -796,6 +811,7 @@ fn emit_program(
|
||||
root_srvs: l.root_srvs,
|
||||
root_uavs: l.root_uavs,
|
||||
root_indexed_views: res.root_indexed_views.clone(),
|
||||
half_views: l.half_views,
|
||||
};
|
||||
Ok((shader, wave_size))
|
||||
}
|
||||
@@ -1578,6 +1594,7 @@ mod tests {
|
||||
root_srvs: Vec::new(),
|
||||
root_uavs: Vec::new(),
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: Vec::new(),
|
||||
};
|
||||
let file = |var: &str, stage| {
|
||||
std::env::var(var).ok().map(|p| super::HostShader {
|
||||
@@ -1590,6 +1607,7 @@ mod tests {
|
||||
root_srvs: Vec::new(),
|
||||
root_uavs: Vec::new(),
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: Vec::new(),
|
||||
})
|
||||
};
|
||||
let ps = file("VICTORY_REF_PS", crate::HwStage::Ps).unwrap_or(ps);
|
||||
@@ -2199,6 +2217,7 @@ mod tests {
|
||||
root_srvs: l.root_srvs,
|
||||
root_uavs: l.root_uavs,
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: Vec::new(),
|
||||
};
|
||||
let (root, pso) = host.compute_pipeline(&cs).unwrap();
|
||||
// Twelve dwords: the loads below run across the view's end.
|
||||
@@ -2352,6 +2371,7 @@ mod tests {
|
||||
root_srvs: l.root_srvs,
|
||||
root_uavs: l.root_uavs,
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: l.half_views,
|
||||
};
|
||||
let (root, pso) = host.compute_pipeline(&cs).unwrap();
|
||||
let inputs: Vec<u32> = [
|
||||
@@ -2768,6 +2788,7 @@ mod barycentric_tests {
|
||||
root_srvs: Vec::new(),
|
||||
root_uavs: Vec::new(),
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: Vec::new(),
|
||||
};
|
||||
validate(&vs);
|
||||
// One triangle covers an 8x8 target, with attribute values 2, 6, 10.
|
||||
|
||||
@@ -106,6 +106,7 @@ fn lowered(native: &NativeProgram, host_wave: u32) -> Result<HostShader, ShaderE
|
||||
root_srvs: result.root_srvs,
|
||||
root_uavs: result.root_uavs,
|
||||
root_indexed_views: res.root_indexed_views,
|
||||
half_views: Vec::new(),
|
||||
};
|
||||
match validator::Validator::load(validator::Validator::default_path().as_deref()) {
|
||||
Ok(mut v) => {
|
||||
|
||||
@@ -34,6 +34,7 @@ fn probe() {
|
||||
root_srvs: Vec::new(),
|
||||
root_uavs: Vec::new(),
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: Vec::new(),
|
||||
};
|
||||
let (root, pso) = match host.compute_pipeline(&shader) {
|
||||
Ok(made) => made,
|
||||
|
||||
@@ -415,6 +415,7 @@ mod tests {
|
||||
root_srvs: Vec::new(),
|
||||
root_uavs: Vec::new(),
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: Vec::new(),
|
||||
};
|
||||
(
|
||||
host(crate::native::HwStage::Vs, vs),
|
||||
|
||||
@@ -34,6 +34,25 @@ pub const VIEW_INTEGER: u32 = 1 << 31;
|
||||
pub const VIEW_SIGNED: u32 = 1 << 30;
|
||||
/// Dwords of the [`SLOT_STRIDES`] buffer: heap indices are clamped to it.
|
||||
pub const SLOT_STRIDES_COUNT: u32 = 1 << 21;
|
||||
/// LOCAL (port of fxceless/victory-fork PR #1216, GamingRipperDoc): the
|
||||
/// [`SLOT_STRIDES`] dword of a slot that holds a constant buffer view (a
|
||||
/// stride no buffer has, no kind bits). A program reads a slot it picks as a
|
||||
/// constant buffer only where this mark is; a slot whose dword is 0 holds
|
||||
/// neither a buffer nor a constant buffer, and the read gives zeros. Read as
|
||||
/// a constant buffer, a texture's descriptor is taken as a buffer's: the
|
||||
/// meshlet culling `_Prev` passes then read whatever lies at a made-up
|
||||
/// address (a page fault on AMD).
|
||||
/// `VICTORY_CONSTANT_MARKS=1` turns it on. Off by default here (on in the
|
||||
/// PR): not yet run in the game on this tree, where a constant buffer slot
|
||||
/// whose mark is not written yet would read zeros.
|
||||
pub const SLOT_STRIDE_CONSTANT: u32 = VIEW_STRIDE_BITS;
|
||||
|
||||
/// Whether [`SLOT_STRIDE_CONSTANT`] is in use: read once, by the host and
|
||||
/// the translator alike.
|
||||
pub fn constant_marks() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var_os("VICTORY_CONSTANT_MARKS").is_some_and(|v| v == "1"))
|
||||
}
|
||||
|
||||
/// Title memory by address (`resource::MEMORY`): console code reads and
|
||||
/// writes through raw 40-bit addresses it takes from root constants,
|
||||
|
||||
@@ -73,6 +73,7 @@ fn build(words: &[u32], srv: (u8, u32)) -> Result<HostShader, ShaderError> {
|
||||
root_srvs: lowered.root_srvs,
|
||||
root_uavs: lowered.root_uavs,
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: Vec::new(),
|
||||
};
|
||||
if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
|
||||
v.validate(&mut shader.dxil.clone())
|
||||
|
||||
@@ -181,6 +181,7 @@ fn build_cs(body: &[u32], threads: u32, lds_bytes: u32) -> HostShader {
|
||||
root_srvs: lowered.root_srvs,
|
||||
root_uavs: lowered.root_uavs,
|
||||
root_indexed_views: res.root_indexed_views,
|
||||
half_views: lowered.half_views,
|
||||
};
|
||||
|
||||
if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
|
||||
@@ -1918,6 +1919,7 @@ fn whole_wave_f64_sum_and_cvt_f32_f64() {
|
||||
root_srvs: lowered.root_srvs,
|
||||
root_uavs: lowered.root_uavs,
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: lowered.half_views,
|
||||
};
|
||||
if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
|
||||
let mut copy = shader.dxil.clone();
|
||||
@@ -2057,3 +2059,488 @@ fn rdna_forms_give_what_the_spelled_out_ones_give() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A program that moves a typed buffer's 16-bit channels
|
||||
/// (`buffer_load_format_d16_xyzw` from the table's first view,
|
||||
/// `buffer_store_format_d16_xyzw` to its second) names both places for the
|
||||
/// host (`HostShader::half_views`) and moves every pattern as it is through
|
||||
/// unsigned views: half floats, the not-a-number ones among them, and
|
||||
/// plain 16-bit integers (the title keeps both in such buffers; the
|
||||
/// network that moves its faces reads indices this way). A program that
|
||||
/// moves no halves names nothing.
|
||||
#[test]
|
||||
fn halves_go_through_a_table_as_they_are_and_name_their_views() {
|
||||
use super::host::Stride;
|
||||
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R16G16B16A16_UINT;
|
||||
/// MUBUF `op vdata, vaddr, s[srsrc:srsrc+3], 0 idxen`.
|
||||
const fn mubuf_idxen(op: u32, vdata: u8, vaddr: u8, srsrc: u8) -> [u32; 2] {
|
||||
[
|
||||
0x38 << 26 | op << 18 | 1 << 13,
|
||||
0x80 << 24 | ((srsrc as u32 >> 2) & 0x1F) << 16 | (vdata as u32) << 8 | vaddr as u32,
|
||||
]
|
||||
}
|
||||
const LOAD_D16_XYZW: u32 = 0x83;
|
||||
const STORE_D16_XYZW: u32 = 0x87;
|
||||
const THREADS: u32 = 32;
|
||||
// The table's second descriptor, 0x20 bytes on.
|
||||
let mut second = s_load_dwordx4(12, 4);
|
||||
second[1] |= 0x20;
|
||||
let mut words = vec![s_mov_b32(5, 0x80)];
|
||||
words.extend_from_slice(&s_load_dwordx4(8, 4));
|
||||
words.extend_from_slice(&second);
|
||||
// v[1:2] = first[thread] (four halves), second[thread] = v[1:2].
|
||||
words.extend_from_slice(&mubuf_idxen(LOAD_D16_XYZW, 1, 0, 8));
|
||||
words.extend_from_slice(&mubuf_idxen(STORE_D16_XYZW, 1, 0, 12));
|
||||
words.push(S_ENDPGM);
|
||||
let view = |kind: ResourceType, table_offset_dwords: u32| UsedResource {
|
||||
kind,
|
||||
root: RootType::Table,
|
||||
table_offset_dwords,
|
||||
lower_bound: table_offset_dwords / 8,
|
||||
stride: 4,
|
||||
dwords: 4,
|
||||
user_data: 4,
|
||||
slot: 1,
|
||||
};
|
||||
let native = NativeProgram {
|
||||
stage: HwStage::Cs,
|
||||
wave64: false,
|
||||
vgprs: 8,
|
||||
layout: vec![
|
||||
RootParam {
|
||||
root: RootType::Constants,
|
||||
table_offset_dwords: 0,
|
||||
sgprs: 4,
|
||||
base_register: 0,
|
||||
user_data: 0,
|
||||
slot: 0,
|
||||
},
|
||||
RootParam {
|
||||
root: RootType::Table,
|
||||
table_offset_dwords: 0,
|
||||
sgprs: 1,
|
||||
base_register: 0,
|
||||
user_data: 4,
|
||||
slot: 1,
|
||||
},
|
||||
],
|
||||
used: vec![view(ResourceType::Srv, 0), view(ResourceType::Uav, 8)],
|
||||
io: StageIo::Cs(ComputeIo {
|
||||
rsrc2: 5 << 1,
|
||||
threads: [THREADS, 1, 1],
|
||||
}),
|
||||
code: words.iter().flat_map(|w| w.to_le_bytes()).collect(),
|
||||
};
|
||||
let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build");
|
||||
let st = structure::structure(&p).expect("structure");
|
||||
let res =
|
||||
resource::track(&p, &native.layout, native.stage, Some(&st)).expect("resource::track");
|
||||
let io = io::raise(&p, &native).expect("io::raise");
|
||||
let lowered = lower::lower(&lower::Parts {
|
||||
native: &native,
|
||||
p: &p,
|
||||
st: &st,
|
||||
io: &io,
|
||||
res: &res,
|
||||
inputs: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
prims: Vec::new(),
|
||||
mesh_stride: 0,
|
||||
})
|
||||
.expect("lower");
|
||||
assert_eq!(lowered.half_views, [(1, 0), (1, 8)]);
|
||||
let shader = HostShader {
|
||||
stage: HwStage::Cs,
|
||||
dxil: lowered.dxil,
|
||||
user_data: lowered.user_data,
|
||||
samplers: lowered.samplers,
|
||||
static_samplers: Vec::new(),
|
||||
root_cbvs: lowered.root_cbvs,
|
||||
root_srvs: lowered.root_srvs,
|
||||
root_uavs: lowered.root_uavs,
|
||||
root_indexed_views: res.root_indexed_views,
|
||||
half_views: lowered.half_views,
|
||||
};
|
||||
if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
|
||||
let mut dxil = shader.dxil.clone();
|
||||
v.validate(&mut dxil).expect("DXIL validation failed");
|
||||
}
|
||||
// A raw store through the same table: no halves, nothing named.
|
||||
assert!(build_valu_cs(&[]).half_views.is_empty());
|
||||
|
||||
// Four halves a thread: numbers, both zeros, subnormals, infinities,
|
||||
// not-a-numbers of both signs, and integers past the largest number's
|
||||
// pattern, as indices are.
|
||||
const SPECIAL: [u16; 16] = [
|
||||
0x0000, 0x0001, 0x03ff, 0x0400, 0x3c00, 0x7bff, 0x7c00, 0x7c01, 0x7e00, 0x7fff, 0x8000,
|
||||
0x8001, 0xbc00, 0xfc00, 0xfc01, 0xffff,
|
||||
];
|
||||
let halves: Vec<u16> = (0..4 * THREADS as usize)
|
||||
.map(|i| match i {
|
||||
0..16 => SPECIAL[i],
|
||||
_ => (i as u16).wrapping_mul(0x0203).wrapping_add(0x7b00),
|
||||
})
|
||||
.collect();
|
||||
let data: Vec<u32> = halves
|
||||
.chunks(2)
|
||||
.map(|pair| u32::from(pair[0]) | u32::from(pair[1]) << 16)
|
||||
.collect();
|
||||
let _one = ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut ran = 0;
|
||||
for (label, warp) in [("hardware", false), ("warp", true)] {
|
||||
let Ok(host) = Host::new(warp) else {
|
||||
eprintln!("no SM 6.6 {label} device; skipped");
|
||||
continue;
|
||||
};
|
||||
let (root, pso) = host
|
||||
.compute_pipeline(&shader)
|
||||
.unwrap_or_else(|e| panic!("{label}: compute_pipeline failed: {e}"));
|
||||
let mut ud = vec![0u32; shader.user_data_dwords().max(1) as usize];
|
||||
for u in &shader.user_data {
|
||||
ud[u.index as usize] = match (u.root, u.slot) {
|
||||
(RootType::Table, 1) => 0,
|
||||
other => panic!("unexpected user data {other:?}"),
|
||||
};
|
||||
}
|
||||
let out = host
|
||||
.dispatch(
|
||||
&root,
|
||||
&pso,
|
||||
&Dispatch {
|
||||
roots: vec![(0, Root::Constants(ud))],
|
||||
views: &[
|
||||
View::Buffer {
|
||||
data: &data,
|
||||
stride: Stride::Format(DXGI_FORMAT_R16G16B16A16_UINT, 8),
|
||||
},
|
||||
View::HalfUintUav(THREADS),
|
||||
],
|
||||
samplers: &[],
|
||||
groups: [1, 1, 1],
|
||||
},
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("{label}: dispatch failed: {e}"));
|
||||
assert_eq!(
|
||||
out[0], data,
|
||||
"{label}: the halves stored are the halves loaded"
|
||||
);
|
||||
ran += 1;
|
||||
}
|
||||
assert!(ran > 0, "no SM 6.6 device available");
|
||||
}
|
||||
|
||||
/// The places a program that moves halves names are the descriptors it
|
||||
/// loads, not the start of the range their used-resource entry describes:
|
||||
/// with one entry for a range from dword 0, a load at byte 0x20 names
|
||||
/// dword 8 (a host view there in a 16-bit float format is the one shown
|
||||
/// as its unsigned twin). A descriptor picked at run time (the load's
|
||||
/// offset in an SGPR) names nothing and keeps the float path: which slot
|
||||
/// it reads, and in which format, is known only then.
|
||||
#[test]
|
||||
fn half_places_are_the_descriptors_the_program_loads() {
|
||||
/// MUBUF `op vdata, vaddr, s[srsrc:srsrc+3], 0 idxen`.
|
||||
const fn mubuf_idxen(op: u32, vdata: u8, vaddr: u8, srsrc: u8) -> [u32; 2] {
|
||||
[
|
||||
0x38 << 26 | op << 18 | 1 << 13,
|
||||
0x80 << 24 | ((srsrc as u32 >> 2) & 0x1F) << 16 | (vdata as u32) << 8 | vaddr as u32,
|
||||
]
|
||||
}
|
||||
const LOAD_D16_XYZW: u32 = 0x83;
|
||||
const STORE_D16_XYZW: u32 = 0x87;
|
||||
let lowered = |words: &[u32]| {
|
||||
let range = |kind: ResourceType, table_offset_dwords: u32| UsedResource {
|
||||
kind,
|
||||
root: RootType::Table,
|
||||
table_offset_dwords,
|
||||
lower_bound: table_offset_dwords / 8,
|
||||
stride: 4,
|
||||
dwords: 4,
|
||||
user_data: 4,
|
||||
slot: 1,
|
||||
};
|
||||
let native = NativeProgram {
|
||||
stage: HwStage::Cs,
|
||||
wave64: false,
|
||||
vgprs: 8,
|
||||
layout: vec![
|
||||
RootParam {
|
||||
root: RootType::Constants,
|
||||
table_offset_dwords: 0,
|
||||
sgprs: 4,
|
||||
base_register: 0,
|
||||
user_data: 0,
|
||||
slot: 0,
|
||||
},
|
||||
RootParam {
|
||||
root: RootType::Table,
|
||||
table_offset_dwords: 0,
|
||||
sgprs: 1,
|
||||
base_register: 0,
|
||||
user_data: 4,
|
||||
slot: 1,
|
||||
},
|
||||
],
|
||||
// One entry for a range of views from dword 0, one at 16.
|
||||
used: vec![range(ResourceType::Srv, 0), range(ResourceType::Uav, 16)],
|
||||
io: StageIo::Cs(ComputeIo {
|
||||
rsrc2: 5 << 1,
|
||||
threads: [32, 1, 1],
|
||||
}),
|
||||
code: words.iter().flat_map(|w| w.to_le_bytes()).collect(),
|
||||
};
|
||||
let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build");
|
||||
let st = structure::structure(&p).expect("structure");
|
||||
let res =
|
||||
resource::track(&p, &native.layout, native.stage, Some(&st)).expect("resource::track");
|
||||
let io = io::raise(&p, &native).expect("io::raise");
|
||||
lower::lower(&lower::Parts {
|
||||
native: &native,
|
||||
p: &p,
|
||||
st: &st,
|
||||
io: &io,
|
||||
res: &res,
|
||||
inputs: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
prims: Vec::new(),
|
||||
mesh_stride: 0,
|
||||
})
|
||||
.expect("lower")
|
||||
};
|
||||
// `s_load_dwordx4 s[dst:dst+3], s[4:5], offset` (soffset null), or with
|
||||
// the offset in `soffset` instead.
|
||||
let at = |dst: u8, offset: u32| {
|
||||
let mut w = s_load_dwordx4(dst, 4);
|
||||
w[1] |= offset;
|
||||
w
|
||||
};
|
||||
let picked = |dst: u8, soffset: u8| {
|
||||
let mut w = s_load_dwordx4(dst, 4);
|
||||
w[1] = u32::from(soffset) << 25;
|
||||
w
|
||||
};
|
||||
let mut fixed = vec![s_mov_b32(5, 0x80)];
|
||||
fixed.extend_from_slice(&at(8, 0x20));
|
||||
fixed.extend_from_slice(&at(12, 0x40));
|
||||
fixed.extend_from_slice(&mubuf_idxen(LOAD_D16_XYZW, 1, 0, 8));
|
||||
fixed.extend_from_slice(&mubuf_idxen(STORE_D16_XYZW, 1, 0, 12));
|
||||
fixed.push(S_ENDPGM);
|
||||
assert_eq!(lowered(&fixed).half_views, [(1, 8), (1, 16)]);
|
||||
// The load's offset in s20, from the first lane's thread index
|
||||
// (`v_readfirstlane_b32 s20, v0`): picked at run time. The pick may land
|
||||
// on the store's place too, so no place of that table is named: the d16
|
||||
// accesses there take the float path, as before half views.
|
||||
let mut runtime = vec![s_mov_b32(5, 0x80), vop1(0x02, 20, vgpr(0))];
|
||||
runtime.extend_from_slice(&picked(8, 20));
|
||||
runtime.extend_from_slice(&at(12, 0x40));
|
||||
runtime.extend_from_slice(&mubuf_idxen(LOAD_D16_XYZW, 1, 0, 8));
|
||||
runtime.extend_from_slice(&mubuf_idxen(STORE_D16_XYZW, 1, 0, 12));
|
||||
runtime.push(S_ENDPGM);
|
||||
assert!(lowered(&runtime).half_views.is_empty());
|
||||
}
|
||||
|
||||
/// A format load of whole dwords (`buffer_load_format_x`, not d16) whose
|
||||
/// result the program only reads one half of as a half float
|
||||
/// (`v_cvt_f32_f16`) is moving 16-bit integers: the head program reads the
|
||||
/// face network's output so, from an `R16_UINT` view. It names its view for
|
||||
/// the host and loads the integer: 0x3c00 comes through as 1.0. Used as a
|
||||
/// whole float instead (`v_add_f32`), the load stays a float load and names
|
||||
/// nothing; so does a load of halves beside a load of the same view read as
|
||||
/// a whole float (a view of numbers the program also picks one half float
|
||||
/// out of, as hundreds of the title's pixel programs do).
|
||||
#[test]
|
||||
fn a_format_load_read_as_halves_loads_the_integer() {
|
||||
use super::host::Stride;
|
||||
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R16_UINT;
|
||||
/// MUBUF `op vdata, vaddr, s[srsrc:srsrc+3], 0 idxen`.
|
||||
const fn mubuf_idxen(op: u32, vdata: u8, vaddr: u8, srsrc: u8) -> [u32; 2] {
|
||||
[
|
||||
0x38 << 26 | op << 18 | 1 << 13,
|
||||
0x80 << 24 | ((srsrc as u32 >> 2) & 0x1F) << 16 | (vdata as u32) << 8 | vaddr as u32,
|
||||
]
|
||||
}
|
||||
const LOAD_FORMAT_X: u32 = 0x00;
|
||||
const V_CVT_F32_F16: u16 = 0x0B;
|
||||
const V_ADD_F32: u8 = 0x03;
|
||||
let lowered = |body: &[u32]| {
|
||||
let view = |kind: ResourceType, table_offset_dwords: u32, stride: u32| UsedResource {
|
||||
kind,
|
||||
root: RootType::Table,
|
||||
table_offset_dwords,
|
||||
lower_bound: table_offset_dwords / 8,
|
||||
stride,
|
||||
dwords: 4,
|
||||
user_data: 4,
|
||||
slot: 1,
|
||||
};
|
||||
let mut second = s_load_dwordx4(12, 4);
|
||||
second[1] |= 0x20;
|
||||
let mut words = vec![s_mov_b32(5, 0x80)];
|
||||
words.extend_from_slice(&s_load_dwordx4(8, 4));
|
||||
words.extend_from_slice(&second);
|
||||
// `body` leaves v2, stored raw.
|
||||
words.extend_from_slice(body);
|
||||
words.extend_from_slice(&buffer_store_dword(2, 12, 0));
|
||||
words.push(S_ENDPGM);
|
||||
let native = NativeProgram {
|
||||
stage: HwStage::Cs,
|
||||
wave64: false,
|
||||
vgprs: 8,
|
||||
layout: vec![
|
||||
RootParam {
|
||||
root: RootType::Constants,
|
||||
table_offset_dwords: 0,
|
||||
sgprs: 4,
|
||||
base_register: 0,
|
||||
user_data: 0,
|
||||
slot: 0,
|
||||
},
|
||||
RootParam {
|
||||
root: RootType::Table,
|
||||
table_offset_dwords: 0,
|
||||
sgprs: 1,
|
||||
base_register: 0,
|
||||
user_data: 4,
|
||||
slot: 1,
|
||||
},
|
||||
],
|
||||
used: vec![view(ResourceType::Srv, 0, 4), view(ResourceType::Uav, 8, 0)],
|
||||
io: StageIo::Cs(ComputeIo {
|
||||
rsrc2: 5 << 1,
|
||||
threads: [1, 1, 1],
|
||||
}),
|
||||
code: words.iter().flat_map(|w| w.to_le_bytes()).collect(),
|
||||
};
|
||||
let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build");
|
||||
let st = structure::structure(&p).expect("structure");
|
||||
let res =
|
||||
resource::track(&p, &native.layout, native.stage, Some(&st)).expect("resource::track");
|
||||
let io = io::raise(&p, &native).expect("io::raise");
|
||||
let l = lower::lower(&lower::Parts {
|
||||
native: &native,
|
||||
p: &p,
|
||||
st: &st,
|
||||
io: &io,
|
||||
res: &res,
|
||||
inputs: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
prims: Vec::new(),
|
||||
mesh_stride: 0,
|
||||
})
|
||||
.expect("lower");
|
||||
HostShader {
|
||||
stage: HwStage::Cs,
|
||||
dxil: l.dxil,
|
||||
user_data: l.user_data,
|
||||
samplers: l.samplers,
|
||||
static_samplers: Vec::new(),
|
||||
root_cbvs: l.root_cbvs,
|
||||
root_srvs: l.root_srvs,
|
||||
root_uavs: l.root_uavs,
|
||||
root_indexed_views: res.root_indexed_views,
|
||||
half_views: l.half_views,
|
||||
}
|
||||
};
|
||||
// v1 = first[v0] (one channel).
|
||||
let load = mubuf_idxen(LOAD_FORMAT_X, 1, 0, 8);
|
||||
let half = vop1(V_CVT_F32_F16, 2, vgpr(1));
|
||||
let as_halves = lowered(&[&load[..], &[half]].concat());
|
||||
assert_eq!(as_halves.half_views, [(1, 0)]);
|
||||
let as_float = lowered(&[&load[..], &[vop2(V_ADD_F32, 2, vgpr(1), 1)]].concat());
|
||||
assert!(as_float.half_views.is_empty());
|
||||
// v3 = first[v0] too, read as a whole float: v2 = half(v1) + v3.
|
||||
let mut beside = load.to_vec();
|
||||
beside.extend_from_slice(&mubuf_idxen(LOAD_FORMAT_X, 3, 0, 8));
|
||||
beside.push(half);
|
||||
beside.push(vop2(V_ADD_F32, 2, vgpr(3), 2));
|
||||
assert!(lowered(&beside).half_views.is_empty());
|
||||
// Behind a branch, as the head program has it: where the lanes meet
|
||||
// again the loaded value goes on through a phi, and a later write of
|
||||
// v1 under EXEC reads it only to keep it in the lanes it leaves alone.
|
||||
const SAVE_EXEC: u32 = 0xBE90_037E; // s_mov_b32 s16, exec_lo
|
||||
const ONLY_LANE_0: u32 = 0x7DA4_0080; // v_cmpx_eq_u32 exec_lo, 0, v0
|
||||
const SKIP_3: u32 = 0xBF88_0003; // s_cbranch_execz over the next 3 words
|
||||
const RESTORE_EXEC: u32 = 0xBEFE_0310; // s_mov_b32 exec_lo, s16
|
||||
let zero = vop1(0x01, 2, 128); // v_mov_b32 v2, 0
|
||||
let rewrite = vop1(0x01, 1, 128); // v_mov_b32 v1, 0
|
||||
let branched = lowered(
|
||||
&[
|
||||
&[zero, SAVE_EXEC, ONLY_LANE_0, SKIP_3][..],
|
||||
&load,
|
||||
&[
|
||||
half,
|
||||
RESTORE_EXEC,
|
||||
SAVE_EXEC,
|
||||
ONLY_LANE_0,
|
||||
rewrite,
|
||||
RESTORE_EXEC,
|
||||
],
|
||||
]
|
||||
.concat(),
|
||||
);
|
||||
assert_eq!(branched.half_views, [(1, 0)]);
|
||||
// A d16 load names its place where it is the only access there...
|
||||
const LOAD_FORMAT_D16_X: u32 = 0x80;
|
||||
let d16 = mubuf_idxen(LOAD_FORMAT_D16_X, 1, 0, 8);
|
||||
let d16_alone = lowered(&[&d16[..], &[half]].concat());
|
||||
assert_eq!(d16_alone.half_views, [(1, 0)]);
|
||||
// ...and not beside a whole float read through the same place, which
|
||||
// would see the unsigned twin: v2 = half(d16 v1) + v3.
|
||||
let mut d16_beside = d16.to_vec();
|
||||
d16_beside.extend_from_slice(&mubuf_idxen(LOAD_FORMAT_X, 3, 0, 8));
|
||||
d16_beside.push(half);
|
||||
d16_beside.push(vop2(V_ADD_F32, 2, vgpr(3), 2));
|
||||
assert!(lowered(&d16_beside).half_views.is_empty());
|
||||
if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
|
||||
for shader in [&as_halves, &branched, &d16_alone] {
|
||||
let mut dxil = shader.dxil.clone();
|
||||
v.validate(&mut dxil).expect("DXIL validation failed");
|
||||
}
|
||||
}
|
||||
// Element 0 of an R16_UINT buffer holds the half 1.0 (0x3c00).
|
||||
let data = [0x0000_3c00u32, 0];
|
||||
let _one = ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut ran = 0;
|
||||
for (label, warp) in [("hardware", false), ("warp", true)] {
|
||||
let Ok(host) = Host::new(warp) else {
|
||||
eprintln!("no SM 6.6 {label} device; skipped");
|
||||
continue;
|
||||
};
|
||||
for (name, shader) in [("straight", &as_halves), ("branched", &branched)] {
|
||||
let (root, pso) = host
|
||||
.compute_pipeline(shader)
|
||||
.unwrap_or_else(|e| panic!("{label}: compute_pipeline failed: {e}"));
|
||||
let mut ud = vec![0u32; shader.user_data_dwords().max(1) as usize];
|
||||
for u in &shader.user_data {
|
||||
ud[u.index as usize] = match (u.root, u.slot) {
|
||||
(RootType::Table, 1) => 0,
|
||||
other => panic!("unexpected user data {other:?}"),
|
||||
};
|
||||
}
|
||||
let out = host
|
||||
.dispatch(
|
||||
&root,
|
||||
&pso,
|
||||
&Dispatch {
|
||||
roots: vec![(0, Root::Constants(ud))],
|
||||
views: &[
|
||||
View::Buffer {
|
||||
data: &data,
|
||||
stride: Stride::Format(DXGI_FORMAT_R16_UINT, 2),
|
||||
},
|
||||
View::RawUav(1),
|
||||
],
|
||||
samplers: &[],
|
||||
groups: [1, 1, 1],
|
||||
},
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("{label}: dispatch failed: {e}"));
|
||||
assert_eq!(
|
||||
out[0][0],
|
||||
1.0f32.to_bits(),
|
||||
"{label}, {name}: the half read as an integer's bits"
|
||||
);
|
||||
}
|
||||
ran += 1;
|
||||
}
|
||||
assert!(ran > 0, "no SM 6.6 device available");
|
||||
}
|
||||
@@ -84,6 +84,7 @@ fn build(words: &[u32], buffers: bool) -> HostShader {
|
||||
root_srvs: lowered.root_srvs,
|
||||
root_uavs: lowered.root_uavs,
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: lowered.half_views,
|
||||
};
|
||||
if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
|
||||
v.validate(&mut shader.dxil.clone())
|
||||
@@ -386,6 +387,7 @@ fn immediate_sampler_chosen_by_control_flow() {
|
||||
root_srvs: lowered.root_srvs,
|
||||
root_uavs: lowered.root_uavs,
|
||||
root_indexed_views: Vec::new(),
|
||||
half_views: Vec::new(),
|
||||
};
|
||||
if let Ok(mut v) =
|
||||
validator::Validator::load(validator::Validator::default_path().as_deref())
|
||||
|
||||
@@ -142,6 +142,7 @@ fn lowered_counted(
|
||||
root_srvs: lowered.root_srvs,
|
||||
root_uavs: lowered.root_uavs,
|
||||
root_indexed_views: res.root_indexed_views,
|
||||
half_views: lowered.half_views,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -281,6 +282,7 @@ fn on_64_lanes(native: &NativeProgram, p: &ir::Program) -> HostShader {
|
||||
root_srvs: lowered.root_srvs,
|
||||
root_uavs: lowered.root_uavs,
|
||||
root_indexed_views: res.root_indexed_views,
|
||||
half_views: Vec::new(),
|
||||
};
|
||||
if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
|
||||
let mut copy = shader.dxil.clone();
|
||||
|
||||
Reference in new issue
Block a user