Shaders: normalized buffer codes give the nearest float, cube gradients take DXIL's base-2 logarithm, d16 data of integer views keeps its bits, unordered views of vertex programs are declared
- VIctory da82e444: the host's division was a step off for some codes, and a material index cut from one was the code below. - DexNom/VIctory 921d8d3b (its cube part, with its test): the level was taken from a natural logarithm DXIL does not have, 1.44 times too high; the instruction's least level was lost. - VIctory 4243a96a, for buffers a program picks from the heap. - VIctory a1a2077a. Every shader is translated anew. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
1cfc476831
commit
31f662bc9b
9 files changed
+501
-36
No files matched your search
@@ -1005,6 +1005,17 @@ fn view_stride(view: &View) -> Option<u32> {
|
||||
})
|
||||
}
|
||||
|
||||
/// What else the table of strides says of a typed buffer view of an integer
|
||||
/// format (`victory_shader::emit::VIEW_INTEGER`); 0 for any other view.
|
||||
fn view_kind(view: &View) -> u32 {
|
||||
use victory_shader::emit::{VIEW_INTEGER, VIEW_SIGNED};
|
||||
match view_elements(view) {
|
||||
Some((0, false, 3 | 7 | 12 | 17 | 25 | 30 | 36 | 42 | 50 | 57 | 62)) => VIEW_INTEGER,
|
||||
Some((0, false, 4 | 8 | 14 | 18 | 32 | 38 | 43 | 52 | 59 | 64)) => VIEW_INTEGER | VIEW_SIGNED,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// A buffer view's structure stride, whether it is raw, and its format;
|
||||
/// `None` if `view` is not a buffer view.
|
||||
fn view_elements(view: &View) -> Option<(u32, bool, u32)> {
|
||||
@@ -2366,12 +2377,13 @@ impl HostHeap {
|
||||
}
|
||||
}
|
||||
let stride = view.and_then(view_stride);
|
||||
let kind = view.map_or(0, view_kind);
|
||||
if let Some((_, _, data)) = &self.strides
|
||||
&& index < victory_shader::emit::SLOT_STRIDES_COUNT
|
||||
{
|
||||
// SAFETY: inside the mapping; shaders read it while we write (a
|
||||
// dword, either value is one the slot had).
|
||||
unsafe { (*data as *mut u32).add(index as usize).write_volatile(stride.unwrap_or(0)) };
|
||||
unsafe { (*data as *mut u32).add(index as usize).write_volatile(stride.unwrap_or(0) | kind) };
|
||||
}
|
||||
let mut buffers = self.buffers.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let was = if stride.is_some() {
|
||||
@@ -4580,6 +4592,22 @@ mod tests {
|
||||
assert!(!same_placed_texture(&low, &View::Shader(0, None)));
|
||||
}
|
||||
|
||||
/// Typed buffer views of integer formats are marked in the table of
|
||||
/// strides; float, normalized, raw and structured ones are not.
|
||||
#[test]
|
||||
fn integer_typed_buffer_views_are_marked_in_the_table_of_strides() {
|
||||
use victory_shader::emit::{VIEW_INTEGER, VIEW_SIGNED};
|
||||
// Format, dimension (buffer), first element, count, stride, counter, flags.
|
||||
let uav = |format, stride, flags| View::Unordered(0, 0, Some([format, 1, 0, 0, 64, stride, 0, 0, flags, 0]));
|
||||
assert_eq!(view_kind(&uav(12, 0, 0)), VIEW_INTEGER, "R16G16B16A16_UINT");
|
||||
assert_eq!(view_kind(&uav(59, 0, 0)), VIEW_INTEGER | VIEW_SIGNED, "R16_SINT");
|
||||
assert_eq!(view_kind(&uav(10, 0, 0)), 0, "R16G16B16A16_FLOAT");
|
||||
assert_eq!(view_kind(&uav(58, 0, 0)), 0, "R16_SNORM");
|
||||
assert_eq!(view_kind(&uav(39, 0, 1)), 0, "raw");
|
||||
assert_eq!(view_kind(&uav(0, 16, 0)), 0, "structured");
|
||||
assert_eq!(view_stride(&uav(12, 0, 0)), Some(8));
|
||||
}
|
||||
|
||||
/// What may stand for a texture that is not there yet: one of its kind in
|
||||
/// another place or size; another one only if asked; nothing a shader
|
||||
/// cannot read in its place.
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
//! Cube samples with gradients select their level by the footprint (after
|
||||
//! DexNom/VIctory 921d8d3b).
|
||||
|
||||
use super::host::{Dispatch, Host, Root, View};
|
||||
use super::intersect_tests::build_compute_shader;
|
||||
use crate::native::{
|
||||
ComputeIo, HwStage, NativeProgram, ResourceType, RootParam, RootType, StageIo, UsedResource,
|
||||
};
|
||||
use windows::Win32::Graphics::Direct3D12::*;
|
||||
|
||||
/// The level is the red channel on all six faces so every sample returns
|
||||
/// its selected mip directly (including the fraction with trilinear filtering).
|
||||
#[test]
|
||||
fn guest_cube_gradient_selects_mip_by_footprint() {
|
||||
const SIZE: usize = 64;
|
||||
const MIPS: u16 = 7;
|
||||
let mut words = vec![
|
||||
0xBE81_0380,
|
||||
0xBE83_0380, // s1, s3 = 0 (upper halves of the tables)
|
||||
0xF40C_0400,
|
||||
0xFA00_0000, // cube SRV to s[16:23] from s[0:1]
|
||||
0xF408_0300,
|
||||
0xFA00_0020, // raw output UAV to s[12:15] from s[0:1]+32
|
||||
0xF408_0601,
|
||||
0xFA00_0000, // sampler to s[24:27] from s[2:3]
|
||||
0x7E1E_0280, // v15 = 0 for output byte offsets
|
||||
];
|
||||
let cases = [
|
||||
// image_sample_d: successive XY footprints (in texels).
|
||||
(0x22, 1.0, 0.0, 0.0),
|
||||
(0x22, 2.0, 0.0, 1.0),
|
||||
(0x22, 16.0, 0.0, 4.0),
|
||||
(0x22, 0.000001, 0.0, 0.0),
|
||||
(0x22, 0.0, 0.0, 0.0),
|
||||
// image_sample_d_cl: an instruction-specified minimum level.
|
||||
(0x23, 1.0, 3.0, 3.0),
|
||||
(0x23, 16.0, 2.0, 4.0),
|
||||
(0x23, 0.0, 2.0, 2.0),
|
||||
// image_sample_l and image_sample_lz: the level is given directly.
|
||||
(0x24, 0.0, 2.0, 2.0),
|
||||
(0x27, 0.0, 0.0, 0.0),
|
||||
];
|
||||
for (i, &(op, footprint, lod, _)) in cases.iter().enumerate() {
|
||||
let delta = footprint / SIZE as f32;
|
||||
let mut addr = Vec::new();
|
||||
if op == 0x22 || op == 0x23 {
|
||||
addr.extend([delta, 0.0, 0.0, delta]); // derivatives along the face
|
||||
}
|
||||
addr.extend([1.5f32, 1.5, 0.0]); // positive X face, center
|
||||
if op == 0x23 || op == 0x24 {
|
||||
addr.push(lod);
|
||||
}
|
||||
for (reg, value) in addr.iter().enumerate() {
|
||||
words.extend([0x7E00_02FF | ((reg as u32) << 17), value.to_bits()]);
|
||||
}
|
||||
// MIMG, CUBE, dmask:R, v0... inputs, SRV s16, sampler s24, result v10.
|
||||
words.extend([0xF000_0118 | (op << 18), 0x00C4_0A00]);
|
||||
// buffer_store_dword v10, v15, s[12:15], 0 offen offset:i*4
|
||||
words.extend([0xE070_1000 | (i as u32 * 4), 0x8003_0A0F]);
|
||||
}
|
||||
words.push(0xBF81_0000); // s_endpgm
|
||||
let table = |slot, user_data| RootParam {
|
||||
root: RootType::Table,
|
||||
table_offset_dwords: 0,
|
||||
sgprs: 1,
|
||||
base_register: 0,
|
||||
user_data,
|
||||
slot,
|
||||
};
|
||||
let range = |kind, at, words, slot, user_data| UsedResource {
|
||||
kind,
|
||||
root: RootType::Table,
|
||||
table_offset_dwords: at,
|
||||
lower_bound: 0,
|
||||
stride: 0,
|
||||
dwords: words,
|
||||
user_data,
|
||||
slot,
|
||||
};
|
||||
let native = NativeProgram {
|
||||
stage: HwStage::Cs,
|
||||
wave64: false,
|
||||
vgprs: 16,
|
||||
layout: vec![table(0, 0), table(1, 2)],
|
||||
used: vec![
|
||||
range(ResourceType::Srv, 0, 8, 0, 0),
|
||||
range(ResourceType::Uav, 8, 4, 0, 0),
|
||||
range(ResourceType::Sampler, 0, 4, 1, 2),
|
||||
],
|
||||
io: StageIo::Cs(ComputeIo {
|
||||
rsrc2: 3 << 1,
|
||||
threads: [1, 1, 1],
|
||||
}),
|
||||
code: words.into_iter().flat_map(u32::to_le_bytes).collect(),
|
||||
};
|
||||
let (shader, _) = build_compute_shader(&native).unwrap();
|
||||
let texels: Vec<_> = (0..6)
|
||||
.flat_map(|_| {
|
||||
(0..MIPS).flat_map(|mip| std::iter::repeat_n([f32::from(mip); 4], (SIZE >> mip).pow(2)))
|
||||
})
|
||||
.collect();
|
||||
let sampler = D3D12_SAMPLER_DESC {
|
||||
Filter: D3D12_FILTER_MIN_MAG_MIP_LINEAR,
|
||||
AddressU: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
|
||||
AddressV: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
|
||||
AddressW: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
|
||||
MaxLOD: f32::MAX,
|
||||
MaxAnisotropy: 1,
|
||||
..Default::default()
|
||||
};
|
||||
for (name, warp) in [("WARP", true), ("hardware", false)] {
|
||||
let Ok(host) = Host::new(warp) else {
|
||||
eprintln!("no SM 6.6 {name} device; skipped");
|
||||
continue;
|
||||
};
|
||||
let (root, pso) = host.compute_pipeline(&shader).unwrap();
|
||||
let out = host
|
||||
.dispatch(
|
||||
&root,
|
||||
&pso,
|
||||
&Dispatch {
|
||||
roots: vec![(
|
||||
0,
|
||||
Root::Constants(vec![0; shader.user_data_dwords() as usize]),
|
||||
)],
|
||||
views: &[
|
||||
View::Cube(SIZE as u32, MIPS, &texels),
|
||||
View::RawUav(cases.len() as u32),
|
||||
],
|
||||
samplers: &[sampler],
|
||||
groups: [1, 1, 1],
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let levels: Vec<_> = out[0].iter().copied().map(f32::from_bits).collect();
|
||||
let expected = cases.map(|(_, _, _, mip)| mip);
|
||||
assert_eq!(levels.as_slice(), expected, "{name}");
|
||||
}
|
||||
}
|
||||
@@ -1164,7 +1164,7 @@ fn translated_compute_matches_pc() {
|
||||
typed_view(DXGI_FORMAT_R8_UNORM, 1),
|
||||
typed_view(DXGI_FORMAT_R16_FLOAT, 2),
|
||||
typed_view(DXGI_FORMAT_R8G8B8A8_SNORM, 4),
|
||||
View::Cube(4, &cube_texels),
|
||||
View::Cube(4, 1, &cube_texels),
|
||||
])
|
||||
.collect();
|
||||
let root_cbv = floats(64, -0.125);
|
||||
|
||||
@@ -994,8 +994,8 @@ fn transition(
|
||||
pub enum View<'a> {
|
||||
/// An RGBA32F texture SRV (width, height, texels).
|
||||
Texture(u32, u32, &'a [[f32; 4]]),
|
||||
/// An RGBA32F cube SRV (face size, texels of faces +x -x +y -y +z -z).
|
||||
Cube(u32, &'a [[f32; 4]]),
|
||||
/// An RGBA32F cube SRV (face size, mip count, texels in D3D subresource order).
|
||||
Cube(u32, u16, &'a [[f32; 4]]),
|
||||
/// A raw buffer UAV of this many dwords, zeroed.
|
||||
RawUav(u32),
|
||||
/// An RGBA32F texture UAV (width, height), zeroed.
|
||||
@@ -1170,20 +1170,23 @@ impl Host {
|
||||
at.ptr += i * view_size;
|
||||
match *v {
|
||||
View::Texture(..) | View::Cube(..) => {
|
||||
let (w, h, texels, faces) = match *v {
|
||||
View::Texture(w, h, t) => (w, h, t, 1),
|
||||
View::Cube(n, t) => (n, n, t, 6),
|
||||
let (w, h, mut texels, faces, mips) = match *v {
|
||||
View::Texture(w, h, t) => (w, h, t, 1, 1),
|
||||
View::Cube(n, mips, t) => (n, n, t, 6, mips),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let mut desc = texture(w, h, D3D12_RESOURCE_FLAG_NONE);
|
||||
desc.DepthOrArraySize = faces as u16;
|
||||
desc.MipLevels = mips;
|
||||
let tex = make(
|
||||
D3D12_HEAP_TYPE_DEFAULT,
|
||||
&desc,
|
||||
D3D12_RESOURCE_STATE_COPY_DEST,
|
||||
)?;
|
||||
let pitch = (w as usize * 16).next_multiple_of(256);
|
||||
for face in 0..faces as usize {
|
||||
for sub in 0..u32::from(mips) * faces {
|
||||
let mip = sub % u32::from(mips);
|
||||
let (w, h) = ((w >> mip).max(1), (h >> mip).max(1));
|
||||
let pitch = (w as usize * 16).next_multiple_of(256);
|
||||
let upload = make(
|
||||
D3D12_HEAP_TYPE_UPLOAD,
|
||||
&buffer((pitch * h as usize) as u64, D3D12_RESOURCE_FLAG_NONE),
|
||||
@@ -1191,9 +1194,10 @@ impl Host {
|
||||
)?;
|
||||
let mut p = std::ptr::null_mut();
|
||||
upload.Map(0, None, Some(&mut p)).map_err(err)?;
|
||||
let texels = &texels[face * (w * h) as usize..];
|
||||
let (level, rest) = texels.split_at((w * h) as usize);
|
||||
texels = rest;
|
||||
for y in 0..h as usize {
|
||||
let row = &texels[y * w as usize..(y + 1) * w as usize];
|
||||
let row = &level[y * w as usize..(y + 1) * w as usize];
|
||||
std::ptr::copy_nonoverlapping(
|
||||
row.as_ptr().cast::<u8>(),
|
||||
p.cast::<u8>().add(y * pitch),
|
||||
@@ -1202,7 +1206,7 @@ impl Host {
|
||||
}
|
||||
upload.Unmap(0, None);
|
||||
let mut at_face = subresource(&tex);
|
||||
at_face.Anonymous.SubresourceIndex = face as u32;
|
||||
at_face.Anonymous.SubresourceIndex = sub;
|
||||
list.CopyTextureRegion(
|
||||
&at_face,
|
||||
0,
|
||||
@@ -1224,7 +1228,7 @@ impl Host {
|
||||
Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
|
||||
Anonymous: D3D12_SHADER_RESOURCE_VIEW_DESC_0 {
|
||||
TextureCube: D3D12_TEXCUBE_SRV {
|
||||
MipLevels: 1,
|
||||
MipLevels: u32::from(mips),
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
|
||||
@@ -162,13 +162,19 @@ fn build(
|
||||
wide: bool,
|
||||
descriptor: Descriptor,
|
||||
) -> Result<(HostShader, resource::Resources), ShaderError> {
|
||||
let native = native(&program(wide, descriptor));
|
||||
build_compute_shader(&native(&program(wide, descriptor)))
|
||||
}
|
||||
|
||||
/// `native`, a compute program, translated (the tests' own way: no container).
|
||||
pub(super) fn build_compute_shader(
|
||||
native: &NativeProgram,
|
||||
) -> Result<(HostShader, resource::Resources), ShaderError> {
|
||||
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))?;
|
||||
let io = io::raise(&p, &native).expect("io::raise");
|
||||
let io = io::raise(&p, native).expect("io::raise");
|
||||
let lowered = lower::lower(&lower::Parts {
|
||||
native: &native,
|
||||
native,
|
||||
p: &p,
|
||||
st: &st,
|
||||
io: &io,
|
||||
|
||||
@@ -6594,10 +6594,7 @@ impl<'a> Lower<'a> {
|
||||
/// typed loads do (channels past the format's read 0, 0, 0, 1).
|
||||
// shortcut: the descriptor's channel swizzle is taken as the identity
|
||||
// (it is for raw views and the descriptors code builds), elements of 4
|
||||
// bytes or more as dword-aligned. Norm formats divide, which the AMD
|
||||
// driver does 1 ULP off the hardware's correctly rounded conversion for
|
||||
// some values; correct it with an integer check if a title compares
|
||||
// converted values exactly.
|
||||
// bytes or more as dword-aligned.
|
||||
fn format_load(&mut self, h: V, off: V, format: u8, channels: u32) -> Result<Vec<V>, Why> {
|
||||
let (bits, numeric) = buffer_format(format).ok_or("a buffer format")?;
|
||||
let c = self.cur;
|
||||
@@ -6660,10 +6657,15 @@ impl<'a> Lower<'a> {
|
||||
// 4 loaded 16_16_UNORM's 65535 as the half 0x3BFF, not 0x3C00.
|
||||
let x = match numeric {
|
||||
Numeric::Unorm => {
|
||||
let f = self.s.m.cast(c, Cast::UiToFp, field, f32t);
|
||||
let top = (1u64 << w) - 1;
|
||||
let max = self.s.m.f32((top as f32).to_bits());
|
||||
let q = self.s.m.bin(c, Bin::SDiv, f, max);
|
||||
let q = if w <= 16 {
|
||||
let q = self.norm_quotient(field, w);
|
||||
self.as_float(q)
|
||||
} else {
|
||||
let f = self.s.m.cast(c, Cast::UiToFp, field, f32t);
|
||||
let max = self.s.m.f32((top as f32).to_bits());
|
||||
self.s.m.bin(c, Bin::SDiv, f, max)
|
||||
};
|
||||
let (high, one) = (self.s.m.i32(top as u32), self.s.m.f32(1.0f32.to_bits()));
|
||||
let at_top = self.s.m.cmp(c, pred::EQ, field, high);
|
||||
let q = self.s.m.select(c, at_top, one, q);
|
||||
@@ -6671,10 +6673,23 @@ impl<'a> Lower<'a> {
|
||||
}
|
||||
Numeric::Snorm => {
|
||||
let s = signed(self);
|
||||
let f = self.s.m.cast(c, Cast::SiToFp, s, f32t);
|
||||
let top = (1u32 << (w - 1)) - 1;
|
||||
let max = self.s.m.f32((top as f32).to_bits());
|
||||
let q = self.s.m.bin(c, Bin::SDiv, f, max);
|
||||
let q = if w <= 17 {
|
||||
// The magnitude's quotient, signed (codes at and
|
||||
// below -top are -1.0 below).
|
||||
let (zero, sign) = (self.s.m.i32(0), self.s.m.i32(0x8000_0000));
|
||||
let neg = self.s.m.cmp(c, pred::SLT, s, zero);
|
||||
let minus = self.s.m.bin(c, Bin::Sub, zero, s);
|
||||
let m = self.s.m.select(c, neg, minus, s);
|
||||
let q = self.norm_quotient(m, w - 1);
|
||||
let sign = self.s.m.select(c, neg, sign, zero);
|
||||
let q = self.s.m.bin(c, Bin::Or, q, sign);
|
||||
self.as_float(q)
|
||||
} else {
|
||||
let f = self.s.m.cast(c, Cast::SiToFp, s, f32t);
|
||||
let max = self.s.m.f32((top as f32).to_bits());
|
||||
self.s.m.bin(c, Bin::SDiv, f, max)
|
||||
};
|
||||
// The lowest value is -1.0 too, as is the one above it.
|
||||
let (high, one) = (self.s.m.i32(top), self.s.m.f32(1.0f32.to_bits()));
|
||||
let at_top = self.s.m.cmp(c, pred::SGE, s, high);
|
||||
@@ -6724,6 +6739,144 @@ impl<'a> Lower<'a> {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The bits of the float nearest to `a / d`, `d = 2^w - 1`, for codes
|
||||
/// `0 <= a < d` and `w <= 16`: what the console's buffer unit gives for
|
||||
/// a normalized code. The host's division is not exact (the RX 6700 XT
|
||||
/// returns 8-bit codes one ulp high and some 16-bit codes one ulp low),
|
||||
/// and titles truncate `x * d` back to the code (a material index in a
|
||||
/// vertex colour's alpha), which a quotient one ulp low turns into the
|
||||
/// code below.
|
||||
///
|
||||
/// `Q = floor(a * 2^32 / d) = a * K + floor(a * r / d)` with `K =
|
||||
/// floor(2^32 / d)` and `r = 2^32 mod d` (no division when `r` is 1:
|
||||
/// widths dividing 32). `a / d` is never exact for `0 < a < d` (`d`
|
||||
/// odd, `r` coprime to it), so `Q` rounds to 24 bits up exactly when
|
||||
/// its highest dropped bit is set, and the result is that integer (at
|
||||
/// most 2^24, so its conversion is exact on any host) scaled by a power
|
||||
/// of two; `Q` itself when it fits, the quotient's further bits then
|
||||
/// lying below half an ulp. That holds for every code of every width but
|
||||
/// 11 and 12 (checked for all codes of widths 2 to 16), which take
|
||||
/// [`Self::exact_norm`]. The host's conversion of a larger integer is
|
||||
/// not relied on: WARP rounds those above 2^31 otherwise.
|
||||
fn norm_quotient(&mut self, a: V, w: u32) -> V {
|
||||
if matches!(w, 11 | 12) {
|
||||
return self.exact_norm(a, w);
|
||||
}
|
||||
let c = self.cur;
|
||||
let d = (1u64 << w) - 1;
|
||||
let (kk, r) = ((1u64 << 32) / d, (1u64 << 32) % d);
|
||||
let k = |l: &mut Self, v: u64| l.s.m.i32(v as u32);
|
||||
let (one, kv) = (k(self, 1), k(self, kk));
|
||||
let mut q = self.s.m.bin(c, Bin::Mul, a, kv);
|
||||
if r != 1 {
|
||||
let (rv, dv) = (k(self, r), k(self, d));
|
||||
let t = self.s.m.bin(c, Bin::Mul, a, rv);
|
||||
let more = self.s.m.bin(c, Bin::UDiv, t, dv);
|
||||
q = self.s.m.bin(c, Bin::Add, q, more);
|
||||
}
|
||||
// The bits to drop (1 to 8) from the exponent of `q >> 8`, which
|
||||
// converts exactly (fewer than 25 bits); none when `q` fits.
|
||||
let k8 = k(self, 8);
|
||||
let top = self.s.m.bin(c, Bin::LShr, q, k8);
|
||||
let top = self.s.m.cast(c, Cast::UiToFp, top, self.s.t.f32);
|
||||
let top = self.as_int(top);
|
||||
let k23 = k(self, 23);
|
||||
let e = self.s.m.bin(c, Bin::LShr, top, k23);
|
||||
let bias = k(self, 127 + 15);
|
||||
let shift = self.s.m.bin(c, Bin::Sub, e, bias);
|
||||
let (zero, b24) = (k(self, 0), k(self, 1 << 24));
|
||||
let fits = self.s.m.cmp(c, pred::IULT, q, b24);
|
||||
let shift = self.s.m.select(c, fits, zero, shift);
|
||||
// Rounded up when the highest dropped bit is set: `a / d` is never
|
||||
// exact, so a dropped half is more than half, never a tie.
|
||||
let up = self.s.m.bin(c, Bin::Shl, q, one);
|
||||
let up = self.s.m.bin(c, Bin::LShr, up, shift);
|
||||
let up = self.s.m.bin(c, Bin::And, up, one);
|
||||
let m = self.s.m.bin(c, Bin::LShr, q, shift);
|
||||
let m = self.s.m.bin(c, Bin::Add, m, up);
|
||||
// At most 2^24: exact as a float, and so its scale by 2^(shift - 32).
|
||||
let f = self.s.m.cast(c, Cast::UiToFp, m, self.s.t.f32);
|
||||
let b95 = k(self, 127 - 32);
|
||||
let scale = self.s.m.bin(c, Bin::Add, shift, b95);
|
||||
let scale = self.s.m.bin(c, Bin::Shl, scale, k23);
|
||||
let scale = self.as_float(scale);
|
||||
let f = self.s.m.bin(c, Bin::Mul, f, scale);
|
||||
self.as_int(f)
|
||||
}
|
||||
|
||||
/// [`Self::norm_quotient`] in integers only, for any width up to 16: `Q`
|
||||
/// as there, 8 more bits when that leaves fewer than 25 (codes of wide
|
||||
/// formats near 0), then rounded to 24 bits to nearest even with the
|
||||
/// remainder as sticky bit, and assembled as float bits.
|
||||
fn exact_norm(&mut self, a: V, w: u32) -> V {
|
||||
let c = self.cur;
|
||||
let d = (1u64 << w) - 1;
|
||||
let (kk, r) = ((1u64 << 32) / d, (1u64 << 32) % d);
|
||||
let k = |l: &mut Self, v: u64| l.s.m.i32(v as u32);
|
||||
let (one, zero, dv, k8) = (k(self, 1), k(self, 0), k(self, d), k(self, 8));
|
||||
let kv = k(self, kk);
|
||||
let mut q = self.s.m.bin(c, Bin::Mul, a, kv);
|
||||
let mut rem = a;
|
||||
if r != 1 {
|
||||
let rv = k(self, r);
|
||||
let t = self.s.m.bin(c, Bin::Mul, a, rv);
|
||||
let more = self.s.m.bin(c, Bin::UDiv, t, dv);
|
||||
q = self.s.m.bin(c, Bin::Add, q, more);
|
||||
rem = self.s.m.bin(c, Bin::URem, t, dv);
|
||||
}
|
||||
// The exponent's bias less the 32 (or 40) fraction bits of `q`; the
|
||||
// shift below is added.
|
||||
let mut bias = k(self, 127 + 23 - 32);
|
||||
if kk < 1 << 24 {
|
||||
// Fewer than 25 bits for the smallest codes: 8 more.
|
||||
let t = self.s.m.bin(c, Bin::Shl, rem, k8);
|
||||
let more = self.s.m.bin(c, Bin::UDiv, t, dv);
|
||||
let rest = self.s.m.bin(c, Bin::URem, t, dv);
|
||||
let wide = self.s.m.bin(c, Bin::Shl, q, k8);
|
||||
let wide = self.s.m.bin(c, Bin::Or, wide, more);
|
||||
let limit = k(self, 1 << 24);
|
||||
let small = self.s.m.cmp(c, pred::IULT, q, limit);
|
||||
q = self.s.m.select(c, small, wide, q);
|
||||
rem = self.s.m.select(c, small, rest, rem);
|
||||
let b40 = k(self, 127 + 23 - 40);
|
||||
bias = self.s.m.select(c, small, b40, bias);
|
||||
}
|
||||
// Bits to drop: the bit length less 24, 1 to 8.
|
||||
let mut shift = one;
|
||||
for bit in 25..32 {
|
||||
let at = k(self, 1 << bit);
|
||||
let ge = self.s.m.cmp(c, pred::IUGE, q, at);
|
||||
let add = self.s.m.select(c, ge, one, zero);
|
||||
shift = self.s.m.bin(c, Bin::Add, shift, add);
|
||||
}
|
||||
let mant = self.s.m.bin(c, Bin::LShr, q, shift);
|
||||
let below = self.s.m.bin(c, Bin::Sub, shift, one);
|
||||
let half = self.s.m.bin(c, Bin::LShr, q, below);
|
||||
let half = self.s.m.bin(c, Bin::And, half, one);
|
||||
let low = self.s.m.bin(c, Bin::Shl, one, below);
|
||||
let low = self.s.m.bin(c, Bin::Sub, low, one);
|
||||
let low = self.s.m.bin(c, Bin::And, q, low);
|
||||
let rest = self.s.m.bin(c, Bin::Or, low, rem);
|
||||
let sticky = self.s.m.cmp(c, pred::NE, rest, zero);
|
||||
let odd = self.s.m.bin(c, Bin::And, mant, one);
|
||||
let tie = self.s.m.select(c, sticky, one, odd);
|
||||
let up = self.s.m.bin(c, Bin::And, half, tie);
|
||||
let mant = self.s.m.bin(c, Bin::Add, mant, up);
|
||||
// Rounding up to 2^24 moves to the next exponent.
|
||||
let k24 = k(self, 24);
|
||||
let carry = self.s.m.bin(c, Bin::LShr, mant, k24);
|
||||
let mant = self.s.m.bin(c, Bin::LShr, mant, carry);
|
||||
let e = self.s.m.bin(c, Bin::Add, bias, shift);
|
||||
let e = self.s.m.bin(c, Bin::Add, e, carry);
|
||||
let k23 = k(self, 23);
|
||||
let e = self.s.m.bin(c, Bin::Shl, e, k23);
|
||||
let fraction = k(self, 0x7F_FFFF);
|
||||
let m = self.s.m.bin(c, Bin::And, mant, fraction);
|
||||
let bits = self.s.m.bin(c, Bin::Or, e, m);
|
||||
let is_zero = self.s.m.cmp(c, pred::EQ, a, zero);
|
||||
self.s.m.select(c, is_zero, zero, bits)
|
||||
}
|
||||
|
||||
/// Channels of buffer format `format`, as [`Self::format_load`] gives
|
||||
/// them, as d16 data: 16 bits each, two per dword, low half first (an
|
||||
/// odd last high half 0). Integer formats keep their low 16 bits; the
|
||||
@@ -7152,6 +7305,33 @@ impl<'a> Lower<'a> {
|
||||
/// `ResourceHeap`): read from the host's table by heap slot in the
|
||||
/// current block.
|
||||
fn slot_stride(&mut self, src: &Source) -> Result<Option<V>, Why> {
|
||||
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)
|
||||
}))
|
||||
}
|
||||
|
||||
/// 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).
|
||||
fn integer_view(&mut self, src: &Source) -> Result<Option<(V, V)>, Why> {
|
||||
let Some(word) = self.slot_word(src)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let c = self.cur;
|
||||
let zero = self.s.m.i32(0);
|
||||
let [int, signed] = [super::shader::VIEW_INTEGER, super::shader::VIEW_SIGNED].map(|bit| {
|
||||
let bit = self.s.m.i32(bit);
|
||||
let x = self.s.m.bin(c, Bin::And, word, bit);
|
||||
self.s.m.cmp(c, pred::NE, x, zero)
|
||||
});
|
||||
Ok(Some((int, signed)))
|
||||
}
|
||||
|
||||
/// The host's word for the buffer view `src` names: its stride and
|
||||
/// format kind ([`super::shader::SLOT_STRIDES`]).
|
||||
fn slot_word(&mut self, src: &Source) -> Result<Option<V>, Why> {
|
||||
let Source::Loaded { load, first } = src else {
|
||||
return Ok(None);
|
||||
};
|
||||
@@ -10938,11 +11118,14 @@ impl<'a> Lower<'a> {
|
||||
let width = self.s.m.extract(c, d, 0);
|
||||
let width = self.s.m.cast(c, Cast::UiToFp, width, self.s.t.f32);
|
||||
let texels = self.s.m.bin(c, Bin::Mul, longer, width);
|
||||
// log2 = natural logarithm / ln 2; a length of 0 gives
|
||||
// DXIL's logarithm is the one to base 2 (it was taken for
|
||||
// the natural one: every level came out 1.44 times too
|
||||
// high; DexNom/VIctory 921d8d3b). A length of 0 gives
|
||||
// minus infinity, which the sampler clamps to level 0.
|
||||
let ln = self.unary(23, texels);
|
||||
let per_ln2 = self.s.m.f32(std::f32::consts::LOG2_E.to_bits());
|
||||
let lod = self.s.m.bin(c, Bin::Mul, ln, per_ln2);
|
||||
// SampleLevel takes no clamp: the instruction's least
|
||||
// level is applied here.
|
||||
let lod = self.unary(23, texels);
|
||||
let lod = if has_cl { self.binary(35, lod, clamp) } else { lod };
|
||||
args.extend(&os);
|
||||
args.push(lod);
|
||||
call_sample(self, "sampleLevel.f32", 62, &args, &mut gathered)
|
||||
@@ -11509,8 +11692,14 @@ impl<'a> Lower<'a> {
|
||||
let mut vals =
|
||||
self.vgprs(inst, data.as_ref().ok_or("buffer store data")?, regs as u8)?;
|
||||
if d16 {
|
||||
// shortcut: d16 data is taken as f16 (integer formats would
|
||||
// keep 16 bits), widened for the 32-bit float store.
|
||||
// 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 };
|
||||
let mut wide = Vec::new();
|
||||
for k in 0..count {
|
||||
let mut x = vals[(k / 2) as usize];
|
||||
@@ -11522,7 +11711,16 @@ impl<'a> Lower<'a> {
|
||||
let f = self
|
||||
.s
|
||||
.op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[x]);
|
||||
wide.push(self.as_int(f));
|
||||
let mut w = self.as_int(f);
|
||||
if let Some((int, signed)) = integer {
|
||||
let (k16, low) = (self.s.m.i32(16), self.s.m.i32(0xFFFF));
|
||||
let zero_ext = self.s.m.bin(c, Bin::And, x, low);
|
||||
let up = self.s.m.bin(c, Bin::Shl, x, k16);
|
||||
let sign_ext = self.s.m.bin(c, Bin::AShr, up, k16);
|
||||
let n = self.s.m.select(c, signed, sign_ext, zero_ext);
|
||||
w = self.s.m.select(c, int, n, w);
|
||||
}
|
||||
wide.push(w);
|
||||
}
|
||||
vals = wide;
|
||||
}
|
||||
@@ -11657,16 +11855,24 @@ impl<'a> Lower<'a> {
|
||||
return Err("a buffer result");
|
||||
};
|
||||
if d16 {
|
||||
// Two f16 channels per VGPR (an odd last high half 0).
|
||||
// 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 };
|
||||
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 h = self
|
||||
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);
|
||||
}
|
||||
x = Some(match x {
|
||||
Some(hi) => {
|
||||
let hi = self.s.m.bin(c, Bin::Shl, hi, k16);
|
||||
|
||||
@@ -83,6 +83,8 @@
|
||||
mod bitcode;
|
||||
mod container;
|
||||
#[cfg(all(test, windows))]
|
||||
mod cube_lod_tests;
|
||||
#[cfg(all(test, windows))]
|
||||
mod diff_tests;
|
||||
#[cfg(all(test, windows))]
|
||||
mod intersect_tests;
|
||||
@@ -118,7 +120,7 @@ pub use lower::{host_settings, set_host_max_wave};
|
||||
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, USER_DISPATCH_THREADS_X, USER_MEMORY_BASE, USER_ROOT_ADDRESS, USER_VIEW_ADDRESS, USER_VIEW_BIAS,
|
||||
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,
|
||||
};
|
||||
|
||||
|
||||
@@ -24,6 +24,14 @@ pub const ROOT_CBV_SPACE: u32 = 1;
|
||||
/// root SRV also tells the host that the buffer views of the whole heap
|
||||
/// must be ready when it runs, not only those its tables name.
|
||||
pub const SLOT_STRIDES: u8 = 255;
|
||||
/// The bits of a [`SLOT_STRIDES`] dword that hold the stride.
|
||||
pub const VIEW_STRIDE_BITS: u32 = (1 << 30) - 1;
|
||||
/// In a [`SLOT_STRIDES`] dword: a typed buffer view of an integer format
|
||||
/// (`UINT` or `SINT`), whose 16-bit typed accesses (`_d16`) pass bits.
|
||||
/// (After VIctory 4243a96a.)
|
||||
pub const VIEW_INTEGER: u32 = 1 << 31;
|
||||
/// With [`VIEW_INTEGER`]: a signed (`SINT`) format.
|
||||
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;
|
||||
|
||||
@@ -93,6 +101,8 @@ pub(crate) mod flags {
|
||||
pub const DOUBLES: u64 = 1 << 2;
|
||||
pub const STENCIL_REF: u64 = 1 << 11;
|
||||
pub const TYPED_UAV_LOADS: u64 = 1 << 13;
|
||||
/// Unordered views in a vertex, geometry or mesh program.
|
||||
pub const UAVS_EVERY_STAGE: u64 = 1 << 16;
|
||||
pub const ROVS: u64 = 1 << 18;
|
||||
pub const WAVE_OPS: u64 = 1 << 19;
|
||||
pub const BARYCENTRICS: u64 = 1 << 22;
|
||||
@@ -557,6 +567,13 @@ impl Shader {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Unordered views outside pixel and compute programs (memory by
|
||||
// address), as DXC counts them: ranges declared below, not views
|
||||
// picked from the heap (VIctory a1a2077a).
|
||||
let compute = self.threads.is_some() && self.mesh.is_none() && self.geometry.is_none();
|
||||
if !self.root_uavs.is_empty() && !self.pixel && !compute {
|
||||
self.flags |= flags::UAVS_EVERY_STAGE;
|
||||
}
|
||||
let mut resources = None;
|
||||
let mut bindings = Vec::new();
|
||||
let i32c = |s: &mut Self, v: u32| {
|
||||
@@ -871,6 +888,9 @@ impl Shader {
|
||||
if self.flags & flags::DOUBLES != 0 {
|
||||
features |= 0x1;
|
||||
}
|
||||
if self.flags & flags::UAVS_EVERY_STAGE != 0 {
|
||||
features |= 0x4;
|
||||
}
|
||||
if self.flags & flags::STENCIL_REF != 0 {
|
||||
features |= 0x200;
|
||||
}
|
||||
|
||||
@@ -415,6 +415,66 @@ fn normalized_buffer_formats_load_their_ends_exactly() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Codes of the normalized formats sampled below: every 8-bit code; for
|
||||
/// 16 bits a stride, the RX 6700 XT's division misses (257, 261, ...) and
|
||||
/// the ends.
|
||||
fn normalized_codes(bits: u32) -> Vec<u32> {
|
||||
if bits <= 10 {
|
||||
return (0..1 << bits).collect();
|
||||
}
|
||||
let edges = [1, 2, 3, 255, 256, 257, 261, 265, 269, 511, 512, 1000, 0x7FFF, 0x8000, 0x8001, 0xFFFE];
|
||||
(0..=0xFFFF).step_by(257).chain(edges).collect()
|
||||
}
|
||||
|
||||
/// Every normalized code loads as the float nearest its exact quotient,
|
||||
/// as the console's buffer unit converts it, not as the host divides (the
|
||||
/// RX 6700 XT gave 125 of the 8-bit codes one ulp high and 16-bit codes
|
||||
/// such as 257 one ulp low): unsigned and signed, 8, 10 (the x channel of
|
||||
/// 2_10_10_10) and 16 bits, the signed formats' two lowest codes -1.0.
|
||||
#[test]
|
||||
fn normalized_buffer_formats_load_correctly_rounded_quotients() {
|
||||
let x = 0;
|
||||
// (format, bits, signed): 8_UNORM, 16_UNORM, 8_SNORM, 16_SNORM,
|
||||
// 2_10_10_10_UNORM and _SNORM.
|
||||
for (format, bits, signed) in [(1u8, 8u32, false), (7, 16, false), (2, 8, true), (8, 16, true), (50, 10, false), (51, 10, true)] {
|
||||
let shader = tbuffer_load_of_v1(x, format);
|
||||
let cases: Vec<[u32; 4]> = normalized_codes(bits).iter().map(|&a| [a, 0, 0, 0]).collect();
|
||||
run_cases(&shader, &cases, |backend, _, [a, ..], got| {
|
||||
let want = if signed {
|
||||
let top = (1i32 << (bits - 1)) - 1;
|
||||
let s = ((a << (32 - bits)) as i32) >> (32 - bits);
|
||||
(s.max(-top) as f32 / top as f32).to_bits()
|
||||
} else {
|
||||
(a as f32 / ((1u32 << bits) - 1) as f32).to_bits()
|
||||
};
|
||||
let sign = if signed { "signed" } else { "unsigned" };
|
||||
assert_eq!(got, want, "{backend}: {bits}-bit {sign} normalized {a:#x} loads as {got:#010x}, want {want:#010x}");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// A title truncates `x * (2^n - 1)` back to the code it stored (the
|
||||
/// material index a mesh program reads from a vertex colour's alpha, as
|
||||
/// `v_mul_legacy_f32` by 255.0 then `v_cvt_u32_f32`): every code comes back
|
||||
/// as itself, on hardware and WARP, which a quotient one ulp low breaks.
|
||||
#[test]
|
||||
fn normalized_codes_truncate_back_to_themselves() {
|
||||
let x = 0;
|
||||
for (format, bits) in [(1u8, 8u32), (7, 16)] {
|
||||
let scale = (((1u32 << bits) - 1) as f32).to_bits();
|
||||
let mut body = buffer_store_dword(1, 8, 0).to_vec();
|
||||
body.extend(mtbuf(x, 0, 8, format));
|
||||
// v_mul_legacy_f32 v0, literal, v0; v_cvt_u32_f32 v0, v0.
|
||||
body.extend([0x07 << 25 | 0xFF, scale]);
|
||||
body.push(vop1(0x07, 0, vgpr(0)));
|
||||
let shader = build_valu_cs(&body);
|
||||
let cases: Vec<[u32; 4]> = normalized_codes(bits).iter().map(|&a| [a, 0, 0, 0]).collect();
|
||||
run_cases(&shader, &cases, |backend, _, [a, ..], got| {
|
||||
assert_eq!(got, a, "{backend}: {bits}-bit code {a} comes back as {got}");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn packed_add_selects_halves_of_inline_constants() {
|
||||
// Add the inline constant 1/(2*pi) to v1 (zero) to read back its two halves.
|
||||
|
||||
Reference in new issue
Block a user