Upstream graphics state fixes: rectangle attributes, sampler bounds, A8 copies, depth footprints

Ports of 6f36cdf (normalized attributes of rectangle lists), 97c8783
(A8_UNORM is not of the R8 typeless family), b956320 (copy footprints of
depth-stencil textures), c1c3281 (the descriptor ring's wrap), e6fe8ec and
e34cb53 (CreateSamplerX bounds), 691236d (appended elements are aligned).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 23:25:15 -06:00
1 parent 6acade55fe
commit fa97cdb3fe
5 files changed
+344 -30

No files matched your search

+26 -2
View File
@@ -861,8 +861,12 @@ impl HostHeap {
let enough = self.free.load(std::sync::atomic::Ordering::Relaxed) >= n
&& n < self.too_long.load(std::sync::atomic::Ordering::Relaxed);
let n = n as usize;
// Search from `next` on, then from the start.
let starts = (*next as usize..self.dynamic as usize).chain(0..*next as usize);
// Search from `next` on, then from the start (extending past cursor by
// n - 1 so contiguous runs crossing the cursor are not missed).
let cursor = *next as usize;
let end = self.dynamic as usize;
let second_end = cursor.saturating_add(n.saturating_sub(1)).min(end);
let starts = (cursor..end).chain(0..second_end);
let mut run = 0;
let mut found = None;
let mut last = usize::MAX;
@@ -2836,4 +2840,24 @@ mod tests {
assert_eq!(heap.dynamic(50), Some(4));
assert_eq!(heap.dynamic(40), Some(4 + heap.dynamic - 40));
}
/// Contiguous free dynamic slot ranges that cross the search cursor are found.
#[test]
fn dynamic_allocation_finds_contiguous_range_crossing_search_cursor() {
let Some(heap) = HostHeap::new(false, 4) else {
return; // no host GPU
};
// Allocate slots 0..8, advancing cursor to 8.
let a = heap.dynamic(8).unwrap();
// Allocate slots 8..16, advancing cursor to 16.
let b = heap.dynamic(8).unwrap();
assert_eq!((a, b), (4, 12));
// Release slots 10..14 (crossing cursor at 12).
// Before the fix, a request of 4 slots starting search at 12 scanned 12..end then 0..12,
// missing the contiguous run at 10..14.
heap.release(14, 4);
let c = heap.dynamic(4);
assert_eq!(c, Some(14));
}
}
+67 -2
View File
@@ -296,13 +296,19 @@ impl GraphicsState {
.map(|e| {
let slot = e.InputSlot.min(31) as usize;
let format = e.Format.0 as u32;
let bytes = crate::resource::element(format).map_or(0, |(b, _)| b as u32);
// An appended element starts at the next multiple of
// min(4, its size) after the previous one, where D3D12 (and
// vkd3d) place it; the host pipeline gets the title's
// descriptions, so the offsets read here must match.
let offset = if e.AlignedByteOffset == D3D12_APPEND_ALIGNED_ELEMENT {
next[slot]
.checked_next_multiple_of(bytes.clamp(1, 4))
.unwrap_or(u32::MAX)
} else {
e.AlignedByteOffset
};
let bytes = crate::resource::element(format).map_or(0, |(b, _)| b as u32);
next[slot] = offset + bytes;
next[slot] = offset.saturating_add(bytes);
Input {
slot: slot as u32,
offset,
@@ -1444,4 +1450,63 @@ mod tests {
assert_eq!(run_draw(0.6, 1.0), 0, "depth 0.5 outside [0.6, 1.0] must be rejected");
assert_eq!(run_draw(0.4, 0.6), 255, "depth 0.5 inside [0.4, 0.6] must pass");
}
fn element(format: DXGI_FORMAT, offset: u32) -> D3D12_INPUT_ELEMENT_DESC {
D3D12_INPUT_ELEMENT_DESC {
SemanticName: PCSTR(c"TEXCOORD".as_ptr().cast()),
SemanticIndex: 0,
Format: format,
InputSlot: 0,
AlignedByteOffset: offset,
InputSlotClass: D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
InstanceDataStepRate: 0,
}
}
fn offsets(inputs: &[D3D12_INPUT_ELEMENT_DESC]) -> Vec<u32> {
let state = GraphicsState {
inputs: inputs.to_vec(),
..GraphicsState::default()
};
state.elements().iter().map(|e| e.offset).collect()
}
/// An appended element after one of 1 or 2 bytes started right after
/// it; D3D12 aligns it to min(4, its size), so the rectangle expansion
/// read its vertices from the wrong bytes.
#[test]
fn appended_elements_are_aligned() {
const APPEND: u32 = D3D12_APPEND_ALIGNED_ELEMENT;
// RG8 (2 bytes) then RG32 float: 4, not 2.
let a = [
element(DXGI_FORMAT_R8G8_UNORM, 0),
element(DXGI_FORMAT_R32G32_FLOAT, APPEND),
];
assert_eq!(offsets(&a), [0, 4]);
// R8 then R16 float: 2, not 1; then R8 packs right after it.
let b = [
element(DXGI_FORMAT_R8_UNORM, 0),
element(DXGI_FORMAT_R16_FLOAT, APPEND),
element(DXGI_FORMAT_R8_UNORM, APPEND),
];
assert_eq!(offsets(&b), [0, 2, 4]);
// Already aligned layouts are unchanged.
let c = [
element(DXGI_FORMAT_R8G8B8A8_UNORM, 0),
element(DXGI_FORMAT_R32G32_FLOAT, APPEND),
element(DXGI_FORMAT_R32G32B32_FLOAT, APPEND),
];
assert_eq!(offsets(&c), [0, 4, 12]);
}
/// An explicit offset near 4 GiB overflowed the next appended offset
/// (a panic in debug builds).
#[test]
fn a_huge_explicit_offset_does_not_overflow() {
let inputs = [
element(DXGI_FORMAT_R32G32B32A32_FLOAT, u32::MAX - 8),
element(DXGI_FORMAT_R32_FLOAT, D3D12_APPEND_ALIGNED_ELEMENT),
];
let _ = offsets(&inputs);
}
}
+46 -2
View File
@@ -55,6 +55,8 @@ enum Kind {
F16,
Unorm8,
Unorm16,
Snorm8,
Snorm16,
}
/// Component count and kind of the vertex formats rectangles use.
@@ -69,7 +71,16 @@ fn components(format: u32) -> Option<(usize, Kind)> {
54 => (1, Kind::F16),
11 => (4, Kind::Unorm16),
35 => (2, Kind::Unorm16),
56 => (1, Kind::Unorm16),
28 | 29 | 87 | 91 => (4, Kind::Unorm8),
49 => (2, Kind::Unorm8),
61 => (1, Kind::Unorm8),
13 => (4, Kind::Snorm16),
37 => (2, Kind::Snorm16),
58 => (1, Kind::Snorm16),
31 => (4, Kind::Snorm8),
51 => (2, Kind::Snorm8),
63 => (1, Kind::Snorm8),
_ => return None,
})
}
@@ -78,8 +89,8 @@ impl Kind {
fn bytes(self) -> usize {
match self {
Kind::F32 => 4,
Kind::F16 | Kind::Unorm16 => 2,
Kind::Unorm8 => 1,
Kind::F16 | Kind::Unorm16 | Kind::Snorm16 => 2,
Kind::Unorm8 | Kind::Snorm8 => 1,
}
}
@@ -90,6 +101,8 @@ impl Kind {
Kind::F16 => half_to_f32(u16::from_le_bytes([b[0], b[1]])),
Kind::Unorm16 => f32::from(u16::from_le_bytes([b[0], b[1]])) / 65535.0,
Kind::Unorm8 => f32::from(b[0]) / 255.0,
Kind::Snorm16 => (f32::from(i16::from_le_bytes([b[0], b[1]])) / 32767.0).max(-1.0),
Kind::Snorm8 => (f32::from(b[0] as i8) / 127.0).max(-1.0),
}
}
@@ -103,6 +116,10 @@ impl Kind {
b.copy_from_slice(&((v.clamp(0.0, 1.0) * 65535.0).round() as u16).to_le_bytes())
}
Kind::Unorm8 => b[0] = (v.clamp(0.0, 1.0) * 255.0).round() as u8,
Kind::Snorm16 => {
b.copy_from_slice(&((v.clamp(-1.0, 1.0) * 32767.0).round() as i16).to_le_bytes())
}
Kind::Snorm8 => b[0] = (v.clamp(-1.0, 1.0) * 127.0).round() as i8 as u8,
}
}
}
@@ -218,6 +235,33 @@ mod tests {
assert_eq!(dst[40..], vertex(4.0, 6.0)[..]);
}
#[test]
fn normalized_attributes_are_interpolated_in_their_own_range() {
let vertex = |rg: [u8; 2], u16_value: u16, u8_value: u8, snorm8: i8, snorm16: i16| {
[
&rg[..],
&u16_value.to_le_bytes(),
&[u8_value, snorm8 as u8],
&snorm16.to_le_bytes(),
]
.concat()
};
let src = [
vertex([3, 250], 1000, 10, i8::MIN, -23000),
vertex([8, 200], 500, 3, -120, -18000),
vertex([20, 253], 10000, 100, -120, -32000),
]
.concat();
let mut dst = [0; 6 * 8];
expand_rects(
&src,
&mut dst,
8,
&[(0, 49), (2, 56), (4, 61), (5, 63), (6, 58)],
);
assert_eq!(dst[40..], vertex([15, 255], 10500, 107, -127, -32767));
}
/// `half_to_f32` reads subnormals, so an f16 rectangle output must be
/// able to write them back instead of flushing every one to zero.
#[test]
+84 -13
View File
@@ -585,7 +585,7 @@ pub(crate) fn family(format: u32) -> u32 {
(44, 47),
(48, 52),
(53, 59),
(60, 65),
(60, 64),
(70, 72),
(73, 75),
(76, 78),
@@ -695,8 +695,7 @@ const NO_FOOTPRINT: PlacedFootprint = PlacedFootprint {
row_pitch: 0,
};
/// One subresource's size in an upload buffer: footprint (offset 0), rows
/// and bytes per row. Planes of depth-stencil formats are not told apart.
/// One subresource's upload footprint (offset 0), row count and unpadded row size.
fn subresource(desc: &ResourceDesc, index: u32) -> Option<(PlacedFootprint, u32, u64)> {
const PITCH: u64 = 256;
if desc.dimension == BUFFER {
@@ -716,11 +715,18 @@ fn subresource(desc: &ResourceDesc, index: u32) -> Option<(PlacedFootprint, u32,
} else {
u32::from(desc.depth_or_array_size).max(1)
};
let (mip, slice) = (index % mips, index / mips);
if slice >= slices {
return None;
}
let (element, block) = element(desc.format)?;
let mip = index % mips;
let plane = index / mips / slices;
let depth_stencil = matches!(desc.format, 19..=22 | 44..=47);
// Each plane contains all the mip levels and array slices. Depth and
// stencil copies use separate formats and 512-byte row alignment.
let (format, pitch_alignment) = match (depth_stencil, plane) {
(true, 0) => (39, 512), // DXGI_FORMAT_R32_TYPELESS
(true, 1) => (60, 512), // DXGI_FORMAT_R8_TYPELESS
(false, 0) => (desc.format, PITCH),
_ => return None,
};
let (element, block) = element(format)?;
let width = (desc.width >> mip).max(1).next_multiple_of(block);
let height = (u64::from(desc.height) >> mip)
.max(1)
@@ -733,18 +739,18 @@ fn subresource(desc: &ResourceDesc, index: u32) -> Option<(PlacedFootprint, u32,
let row_size = width / block * element;
let footprint = PlacedFootprint {
offset: 0,
format: desc.format,
format,
width: u32::try_from(width).ok()?,
height: u32::try_from(height).ok()?,
depth,
row_pitch: u32::try_from(row_size.next_multiple_of(PITCH)).ok()?,
row_pitch: u32::try_from(row_size.next_multiple_of(pitch_alignment)).ok()?,
};
Some((footprint, u32::try_from(height / block).ok()?, row_size))
}
/// Device method: where each subresource goes in an upload buffer, rows
/// 256-byte and subresources 512-byte aligned, as on PC. Unknown sizes give
/// the documented all-ones values.
/// Places subresources at 512-byte boundaries in an upload buffer.
/// Row pitch is aligned to 512 bytes for depth/stencil planes and 256 bytes
/// otherwise. Unknown sizes give the documented all-ones values.
pub(crate) extern "system" fn copyable_footprints(
_: This,
desc: *const ResourceDesc,
@@ -2559,6 +2565,22 @@ mod tests {
use super::*;
use crate::object::slot;
#[test]
fn a8_unorm_is_not_in_the_r8_typeless_family() {
// DXGI_FORMAT values: 60 R8_TYPELESS, 61 R8_UNORM, 62 R8_UINT,
// 63 R8_SNORM, 64 R8_SINT, 65 A8_UNORM. The R8 typeless family is
// 60..=64; A8_UNORM has no typeless family and cannot be cast to R8,
// so it must stand alone rather than fold into R8_TYPELESS.
assert_eq!(family(60), 60);
for f in 61..=64 {
assert_eq!(family(f), 60);
}
assert_eq!(family(65), 65);
// Boundaries: 59 belongs to the preceding family, 66 is unmapped.
assert_eq!(family(59), 53);
assert_eq!(family(66), 66);
}
type Release = extern "system" fn(This) -> u32;
type Map = extern "system" fn(This, u32, *const c_void, *mut *mut u8) -> HRESULT;
type Address = extern "system" fn(This) -> u64;
@@ -2790,6 +2812,55 @@ mod tests {
assert_eq!(t, u64::MAX, "no second subresource");
}
#[test]
fn depth_stencil_footprints_follow_the_pc_plane_order() {
// Native D3D12 reports the same layout for both depth/stencil families
// and their view formats. Mips vary fastest, then array slices, then planes.
let expected = [
(512, 39, 16, 8),
(4_608, 39, 8, 4),
(6_656, 39, 16, 8),
(10_752, 39, 8, 4),
(12_800, 60, 16, 8),
(16_896, 60, 8, 4),
(18_944, 60, 16, 8),
(23_040, 60, 8, 4),
];
for format in (19..=22).chain(44..=47) {
let desc = ResourceDesc {
depth_or_array_size: 2,
..texture(16, 8, format, 2)
};
let mut layouts = [NO_FOOTPRINT; 8];
let (mut rows, mut sizes, mut total) = ([0; 8], [0; 8], 0);
copyable_footprints(
std::ptr::null(),
&desc,
0,
8,
512,
layouts.as_mut_ptr(),
rows.as_mut_ptr(),
sizes.as_mut_ptr(),
&mut total,
);
assert_eq!(
layouts.map(|l| (l.offset, l.format, l.width, l.height)),
expected,
"format {format}"
);
assert!(
layouts.iter().all(|l| l.depth == 1 && l.row_pitch == 512),
"format {format}"
);
assert_eq!(rows, [8, 4, 8, 4, 8, 4, 8, 4], "format {format}");
assert_eq!(sizes, [64, 32, 64, 32, 16, 8, 16, 8], "format {format}");
assert_eq!(total, 24_072, "format {format}");
assert!(subresource(&desc, 8).is_none(), "format {format}");
}
}
/// A 4x4 texture filled from a title buffer through our list and queue,
/// then read back from the host GPU.
#[cfg(windows)]
+121 -11
View File
@@ -47,30 +47,58 @@ impl Fields {
} else {
linear(self.min) << 4 | linear(self.mag) << 2 | u32::from(self.mip == 2)
};
let min_lod = finite_lod(self.min_lod, 0.0);
// Without a mip filter the console clamps sampling to `min_lod`.
let max_lod = if self.mip == 0 {
min_lod
} else {
finite_lod(self.max_lod, min_lod).max(min_lod)
};
D3D12_SAMPLER_DESC {
Filter: D3D12_FILTER((reduction << 7 | filter) as i32),
AddressU: D3D12_TEXTURE_ADDRESS_MODE(self.address[0] as i32),
AddressV: D3D12_TEXTURE_ADDRESS_MODE(self.address[1] as i32),
AddressW: D3D12_TEXTURE_ADDRESS_MODE(self.address[2] as i32),
MipLODBias: self.bias,
AddressU: address_mode(self.address[0]),
AddressV: address_mode(self.address[1]),
AddressW: address_mode(self.address[2]),
MipLODBias: if self.bias.is_nan() {
0.0
} else {
self.bias
.clamp(D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX)
},
MaxAnisotropy: if aniso {
self.anisotropy.clamp(1, 16)
} else {
1
},
ComparisonFunc: D3D12_COMPARISON_FUNC(self.compare.max(1) as i32),
BorderColor: self.border,
MinLOD: self.min_lod,
// Without a mip filter the console samples mip 0 only.
MaxLOD: if self.mip == 0 {
self.min_lod
ComparisonFunc: if reduction == 1 {
D3D12_COMPARISON_FUNC(self.compare as i32)
} else {
self.max_lod
D3D12_COMPARISON_FUNC_NEVER
},
BorderColor: self.border,
MinLOD: min_lod,
MaxLOD: max_lod,
}
}
}
/// Maps a console address mode (`1..=5`) to host D3D12, falling back to `CLAMP`.
fn address_mode(mode: u32) -> D3D12_TEXTURE_ADDRESS_MODE {
match mode {
1..=5 => D3D12_TEXTURE_ADDRESS_MODE(mode as i32),
_ => D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
}
}
/// Sanitizes an LOD bound for D3D12, substituting `fallback` for `NaN` and clamping infinities.
fn finite_lod(value: f32, fallback: f32) -> f32 {
if value.is_nan() {
fallback
} else {
value.clamp(-D3D12_FLOAT32_MAX, D3D12_FLOAT32_MAX)
}
}
/// The console's sampler description (27 words): flags, magnification,
/// minification, mip and depth filters, filter mode, three address modes,
/// minimum and maximum LOD, LOD bias, secondary bias, anisotropy, its bias
@@ -116,4 +144,86 @@ mod tests {
w[16] = 1;
assert_eq!(console(&w).Filter.0, 0x5);
}
#[test]
fn mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps() {
let mut w = [0u32; 27];
w[3] = 0; // mip none
w[9] = 3.0f32.to_bits();
w[10] = 8.0f32.to_bits();
let d = console(&w);
assert_eq!((d.MinLOD, d.MaxLOD), (3.0, 3.0));
w[9] = f32::NAN.to_bits();
w[10] = 8.0f32.to_bits();
let d = console(&w);
assert_eq!((d.MinLOD, d.MaxLOD), (0.0, 0.0));
w[9] = f32::INFINITY.to_bits();
w[10] = 8.0f32.to_bits();
let d = console(&w);
assert_eq!((d.MinLOD, d.MaxLOD), (D3D12_FLOAT32_MAX, D3D12_FLOAT32_MAX));
w[3] = 2; // linear mips
w[9] = 4.0f32.to_bits();
w[10] = 1.0f32.to_bits();
let d = console(&w);
assert_eq!((d.MinLOD, d.MaxLOD), (4.0, 4.0));
w[9] = f32::NAN.to_bits();
w[10] = f32::INFINITY.to_bits();
let d = console(&w);
assert_eq!((d.MinLOD, d.MaxLOD), (0.0, D3D12_FLOAT32_MAX));
w[9] = 2.5f32.to_bits();
w[10] = f32::NAN.to_bits();
let d = console(&w);
assert_eq!((d.MinLOD, d.MaxLOD), (2.5, 2.5));
w[9] = f32::NEG_INFINITY.to_bits();
w[10] = f32::NEG_INFINITY.to_bits();
let d = console(&w);
assert_eq!(
(d.MinLOD, d.MaxLOD),
(-D3D12_FLOAT32_MAX, -D3D12_FLOAT32_MAX)
);
}
#[test]
fn bias_address_and_comparison_stay_in_host_bounds() {
let mut w = [0u32; 27];
w[6..9].copy_from_slice(&[0, 6, 3]);
w[11] = 24.0f32.to_bits();
w[16] = 99;
let d = console(&w);
assert_eq!(d.MipLODBias, D3D12_MIP_LOD_BIAS_MAX);
assert_eq!(
[d.AddressU, d.AddressV, d.AddressW],
[D3D12_TEXTURE_ADDRESS_MODE_CLAMP; 3]
);
assert_eq!(d.ComparisonFunc, D3D12_COMPARISON_FUNC_NEVER);
w[11] = (-24.0f32).to_bits();
w[5] = 1; // minimum reduction
w[16] = 4; // less-or-equal, ignored when reduction != 1
let d = console(&w);
assert_eq!(d.MipLODBias, D3D12_MIP_LOD_BIAS_MIN);
assert_eq!(d.ComparisonFunc, D3D12_COMPARISON_FUNC_NEVER);
w[11] = f32::NAN.to_bits();
w[5] = 0; // plain mode, reduction == 1 from compare == 4
w[6..9].copy_from_slice(&[1, 4, 5]);
w[16] = 4;
let d = console(&w);
assert_eq!(d.MipLODBias, 0.0);
assert_eq!(
[d.AddressU, d.AddressV, d.AddressW],
[
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
D3D12_TEXTURE_ADDRESS_MODE_BORDER,
D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE,
]
);
assert_eq!(d.ComparisonFunc, D3D12_COMPARISON_FUNC_LESS_EQUAL);
}
}