mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 03:39:52 +00:00
metal: cover BC2/BC3 edge blocks and report missing BC support apart
The section 12 audit of #214 found three gaps: no level narrower than a block row (Metal's zero-stride single-row upload) or of 1x1, no checks for zero extents or oversized and block-count-mismatched payloads, and a missing BC capability surfaced the same way as an implementation fault. Adds a 5x3 and 1x1 GPU test with hand-derived anchors, the extra upload rejections, and MetalError::is_capability_unavailable; the GPU tests now print CAPABILITY UNAVAILABLE or IMPLEMENTATION FAILURE. Rounding the texture to whole blocks, accepting oversized payloads, or a device without BC support each makes the tests fail with the matching message. Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
This commit is contained in:
1 parent
0cac6c4fcf
commit
97ea2541bb
5 files changed
+148
-6
No files matched your search
@@ -19,6 +19,10 @@ remains unsupported.
|
||||
orders, padded row and slice pitches, two layers and two mips against the CPU decoders and
|
||||
hand-derived anchors. On an Apple M5 Pro the native decoder's interpolated values were within
|
||||
1.14 codes of the exact values, so up to two codes from the truncating CPU reference.
|
||||
Edge levels of 5×3 (one block row) and 1×1 are covered, invalid payloads (zero extents,
|
||||
short, oversized or block-count-mismatched buffers, bad pitches) are rejected before upload,
|
||||
and `MetalError::is_capability_unavailable` separates a device without BC support from an
|
||||
implementation failure.
|
||||
- Added CPU block decoders for BC2, BC3, BC4 and BC5 (UNORM and SNORM) in `xenora-texture`, with tests whose expected texels were derived by hand from the Microsoft and Khronos block-compression specifications. Interpolated values truncate toward zero, as the existing BC1 decoder does; GPU upload and sampling of these formats are not implemented yet.
|
||||
- Added `dbt::tests::sib_store_executes_through_the_native_dbt`, an independent DBT execution
|
||||
test that runs the SIB store form `mov [rsp + 0x20], rbx` through the native ARM64 DBT and
|
||||
|
||||
@@ -88,6 +88,17 @@ pub struct MetalError {
|
||||
}
|
||||
|
||||
impl MetalError {
|
||||
/// Operation name of the error returned when the device lacks BC texture
|
||||
/// compression, which is a hardware capability, not an implementation fault.
|
||||
pub const BC_UNAVAILABLE: &'static str = "supportsBCTextureCompression";
|
||||
|
||||
/// Whether the device lacks a required capability, as opposed to a failure
|
||||
/// in Xenora's validation or Metal execution.
|
||||
#[must_use]
|
||||
pub fn is_capability_unavailable(&self) -> bool {
|
||||
self.operation == Self::BC_UNAVAILABLE
|
||||
}
|
||||
|
||||
pub(crate) fn new(operation: &'static str, detail: impl Into<String>) -> Self {
|
||||
Self {
|
||||
operation,
|
||||
@@ -332,7 +343,7 @@ kernel void sample_rgba8(texture2d<float, access::sample> tex [[texture(0)]],
|
||||
.ok_or_else(|| MetalError::new("texture bounds", "expected 1..=4096 texels"))?;
|
||||
if !self.device.supportsBCTextureCompression() {
|
||||
return Err(MetalError::new(
|
||||
"supportsBCTextureCompression",
|
||||
MetalError::BC_UNAVAILABLE,
|
||||
format!("{name} is unavailable"),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
//! decoder is the value under test, so expected and actual values come from
|
||||
//! different decoders.
|
||||
|
||||
use super::context;
|
||||
use super::{context, native};
|
||||
use xenora_texture::{BcAlphaFormat, BcAlphaUpload, BcAlphaUploadDesc, decode_bc2, decode_bc3};
|
||||
|
||||
/// The specifications define colour and BC3 alpha interpolation in real
|
||||
@@ -156,11 +156,11 @@ fn read_and_compare(
|
||||
let mut worst = 0;
|
||||
let reads = std::array::from_fn(|layer| {
|
||||
let decoded = layers[layer].map(decode);
|
||||
let base = gpu.read_bc_alpha_upload(&upload, layer, 0).unwrap();
|
||||
let base = native(gpu.read_bc_alpha_upload(&upload, layer, 0));
|
||||
let expected = reference(&decoded[..4], 2, WIDTH, HEIGHT);
|
||||
let label = format!("{format:?} layer {layer}");
|
||||
worst = worst.max(assert_close(&format!("{label} mip 0"), &base, &expected));
|
||||
let small = gpu.read_bc_alpha_upload(&upload, layer, 1).unwrap();
|
||||
let small = native(gpu.read_bc_alpha_upload(&upload, layer, 1));
|
||||
let expected = reference(&decoded[4..], 1, 2, 2);
|
||||
worst = worst.max(assert_close(&format!("{label} mip 1"), &small, &expected));
|
||||
[base, small]
|
||||
@@ -285,3 +285,88 @@ fn reads_pitched_bc3_interpolated_alpha_across_mips_and_layers() {
|
||||
assert_near("six-value index 3", small[3], 92.0);
|
||||
println!("checked 58 BC3 texels from four subresources, worst channel difference {worst}");
|
||||
}
|
||||
|
||||
/// Reads one-mip, one-layer uploads of `width`×`height` from `blocks` laid
|
||||
/// out with `row_pitch`, comparing with the CPU decoder.
|
||||
fn read_edge(
|
||||
format: BcAlphaFormat,
|
||||
decode: fn([u8; 16]) -> [[u8; 4]; 16],
|
||||
blocks: &[[u8; 16]],
|
||||
(width, height, row_pitch): (usize, usize, usize),
|
||||
) -> Vec<u32> {
|
||||
let bytes = blocks.as_flattened();
|
||||
let upload = BcAlphaUpload::new(
|
||||
bytes,
|
||||
BcAlphaUploadDesc {
|
||||
width,
|
||||
height,
|
||||
mips: 1,
|
||||
layers: 1,
|
||||
format,
|
||||
row_pitch,
|
||||
slice_pitch: bytes.len(),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let actual = native(context().read_bc_alpha_upload(&upload, 0, 0));
|
||||
let decoded: Vec<_> = blocks.iter().copied().map(decode).collect();
|
||||
let expected = reference(&decoded, width.div_ceil(4), width, height);
|
||||
assert_close(&format!("{format:?} {width}x{height}"), &actual, &expected);
|
||||
actual
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_bc2_and_bc3_edge_blocks() {
|
||||
let ramp = [0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe];
|
||||
let reverse = ramp.map(|byte: u8| !byte);
|
||||
let [descending, equal] = [DESCENDING, EQUAL].map(colour);
|
||||
// 5×3 is one row of two blocks: Metal receives a zero row stride, and
|
||||
// both blocks are cut at row 3, the second also at column 1. The row
|
||||
// pitch keeps one padding block.
|
||||
let wide = (5, 3, 48);
|
||||
let bc2 = read_edge(
|
||||
BcAlphaFormat::Bc2,
|
||||
decode_bc2,
|
||||
&[block(ramp, descending), block(reverse, equal), PAD],
|
||||
wide,
|
||||
);
|
||||
// (0, 0) red, nibble 0; (4, 0) green, reversed nibble 15; (0, 2) red,
|
||||
// nibble 8; (4, 2) green, reversed nibble 7.
|
||||
assert_eq!(
|
||||
[bc2[0], bc2[4], bc2[10], bc2[14]],
|
||||
[0x0000_00ff, 0xff00_ff00, 0x8800_00ff, 0x7700_ff00]
|
||||
);
|
||||
let bc3 = read_edge(
|
||||
BcAlphaFormat::Bc3,
|
||||
decode_bc3,
|
||||
&[
|
||||
block(bc3_alpha(200, 20), descending),
|
||||
block(bc3_alpha(20, 200), equal),
|
||||
PAD,
|
||||
],
|
||||
wide,
|
||||
);
|
||||
// Row 0 of block 0 is indices 6, 7, 0, 1: (2, 0) and (3, 0) are the
|
||||
// endpoints 200 and 20. Column 0 of block 1 is indices 6, 2, 6: green
|
||||
// with the fixed 0, then (4 * 20 + 200) / 5 = 56.
|
||||
assert_eq!([alpha(bc3[2]), alpha(bc3[3])], [200, 20]);
|
||||
assert_eq!([bc3[4], bc3[14]], [0x0000_ff00; 2]);
|
||||
assert_near("six-value index 2", alpha(bc3[9]), 56.0);
|
||||
// 1×1 is a single block cut to its first texel.
|
||||
let one = (1, 1, 16);
|
||||
let bc2 = read_edge(
|
||||
BcAlphaFormat::Bc2,
|
||||
decode_bc2,
|
||||
&[block(reverse, descending)],
|
||||
one,
|
||||
);
|
||||
assert_eq!(bc2, [0xff00_00ff]);
|
||||
let bc3 = read_edge(
|
||||
BcAlphaFormat::Bc3,
|
||||
decode_bc3,
|
||||
&[block(bc3_alpha(20, 200), descending)],
|
||||
one,
|
||||
);
|
||||
assert_eq!(bc3, [0x0000_00ff]);
|
||||
println!("checked 32 BC2 and BC3 texels from 5x3 and 1x1 edge levels");
|
||||
}
|
||||
@@ -31,6 +31,17 @@ fn context() -> NativeCompute {
|
||||
NativeCompute::new().expect("native Metal device and queue")
|
||||
}
|
||||
|
||||
/// Unwraps a native result, reporting a missing device capability apart from
|
||||
/// an implementation failure. Both fail the test; neither counts as a pass.
|
||||
fn native<T>(result: Result<T, xenora_metal::MetalError>) -> T {
|
||||
result.unwrap_or_else(|error| {
|
||||
if error.is_capability_unavailable() {
|
||||
panic!("CAPABILITY UNAVAILABLE on this device, not an implementation failure: {error}")
|
||||
}
|
||||
panic!("IMPLEMENTATION FAILURE: {error}")
|
||||
})
|
||||
}
|
||||
|
||||
fn verify_words(body: &str, input: &[u32], expected: &[u32]) {
|
||||
let actual = context()
|
||||
.run_words(&fixture(body), "fixture", input)
|
||||
|
||||
@@ -341,15 +341,46 @@ mod tests {
|
||||
mips: 4,
|
||||
..ALPHA_DESC
|
||||
},
|
||||
BcAlphaUploadDesc {
|
||||
width: 0,
|
||||
..ALPHA_DESC
|
||||
},
|
||||
BcAlphaUploadDesc {
|
||||
height: 0,
|
||||
..ALPHA_DESC
|
||||
},
|
||||
BcAlphaUploadDesc {
|
||||
layers: 0,
|
||||
..ALPHA_DESC
|
||||
},
|
||||
BcAlphaUploadDesc {
|
||||
mips: 0,
|
||||
..ALPHA_DESC
|
||||
},
|
||||
] {
|
||||
assert_eq!(
|
||||
BcAlphaUpload::new(&bytes, desc).unwrap_err(),
|
||||
TextureError::InvalidLayout
|
||||
);
|
||||
}
|
||||
// Short, oversized, and one block too few or too many for the layout.
|
||||
let long = [0; 449];
|
||||
for bytes in [&bytes[..447], &long[..], &bytes[..432], &[0; 464][..]] {
|
||||
assert_eq!(
|
||||
BcAlphaUpload::new(bytes, ALPHA_DESC).unwrap_err(),
|
||||
TextureError::InvalidByteLength
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
BcAlphaUpload::new(&bytes[..447], ALPHA_DESC).unwrap_err(),
|
||||
TextureError::InvalidByteLength
|
||||
BcAlphaUpload::new(
|
||||
&bytes,
|
||||
BcAlphaUploadDesc {
|
||||
width: usize::MAX,
|
||||
..ALPHA_DESC
|
||||
}
|
||||
)
|
||||
.unwrap_err(),
|
||||
TextureError::SizeOverflow
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user