mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 19:30:43 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14c52f2e05 | ||
|
|
a6b0e89cac |
No files matched your search
@@ -29,6 +29,7 @@ remains unsupported.
|
||||
- Added a Python 3 fallback for `timeout` in `scripts/test-gpu.sh`, so the native Metal suite runs on stock macOS without GNU coreutils while preserving bounded execution and the usual timeout exit status.
|
||||
|
||||
### Added
|
||||
- Added checked, per-mip copy footprints for synthetic block-compressed texture arrays. Contributors can pack tight block rows or repack entire chains with independent row and placement alignment, and pass selected BC1–BC5 levels to the existing Metal readers (#391).
|
||||
- `dbt_bench` can also write its results as one JSON document when `XENORA_DBT_BENCH_JSON=<path>` is set (#112). The document records the host CPU, OS and architecture, the git revision and whether the tree had local changes, debug assertions, the workload and sample counts, the median, minimum, maximum, p95, p99, IQR and MIPS for the interpreter and for cold and warm DBT, and the cold DBT counters. Anything the benchmark cannot determine is `null`. The text output and the timed work are unchanged.
|
||||
- Added synthetic guest handle-lifetime regression tests (#205) for capacity reuse, stale handles, duplicate rights and lifetime, wrong object types, thread pseudo handles and failed-output nonmutation. Runtime behavior is unchanged; independent acceptance is pending.
|
||||
- Added a documented independent acceptance rule for promoting AMD RDNA2 desktop GPU (gfx1030) forms from local CPU and Metal results to `fixture verified`, including clean-commit evidence, reviewer independence, raw-log location, review-log summary, failure handling and status regeneration (#302).
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#![cfg(all(feature = "gpu-tests", target_arch = "aarch64", target_os = "macos"))]
|
||||
|
||||
use xenora_metal::{MetalError, NativeCompute};
|
||||
use xenora_texture::{
|
||||
BcAlphaFormat, BcChannelFormat, BcCopyAlignment, BcFormat, BcTextureDesc, BcTextureLayout,
|
||||
PackedBcUpload,
|
||||
};
|
||||
|
||||
const EXTENTS: [(usize, usize); 4] = [(9, 5), (4, 2), (2, 1), (1, 1)];
|
||||
const BLOCK_COUNTS: [usize; 4] = [6, 1, 1, 1];
|
||||
|
||||
// Zero selector bits pick the first endpoint. These RGB565 endpoints encode
|
||||
// exact red, green, blue and white.
|
||||
const COLORS: [[u8; 2]; 4] = [[0x00, 0xf8], [0xe0, 0x07], [0x1f, 0x00], [0xff, 0xff]];
|
||||
const PIXELS: [[u8; 4]; 4] = [
|
||||
[255, 0, 0, 255],
|
||||
[0, 255, 0, 255],
|
||||
[0, 0, 255, 255],
|
||||
[255, 255, 255, 255],
|
||||
];
|
||||
|
||||
fn append_block(bytes: &mut Vec<u8>, format: BcFormat, variant: usize) {
|
||||
let color = COLORS[variant];
|
||||
let color_block = [color[0], color[1], 0, 0, 0, 0, 0, 0];
|
||||
let endpoint_block = |first| [first, 0, 0, 0, 0, 0, 0, 0];
|
||||
match format {
|
||||
BcFormat::Bc1 => bytes.extend(color_block),
|
||||
BcFormat::BcAlpha(format) => {
|
||||
bytes.extend(match format {
|
||||
BcAlphaFormat::Bc2 => [0xff; 8],
|
||||
BcAlphaFormat::Bc3 => endpoint_block(255),
|
||||
});
|
||||
bytes.extend(color_block);
|
||||
}
|
||||
BcFormat::BcChannel(format) => match format {
|
||||
BcChannelFormat::Bc4Unorm => bytes.extend(endpoint_block(PIXELS[variant][0])),
|
||||
BcChannelFormat::Bc5Unorm => {
|
||||
bytes.extend(endpoint_block(PIXELS[variant][0]));
|
||||
bytes.extend(endpoint_block(PIXELS[variant][1]));
|
||||
}
|
||||
BcChannelFormat::Bc4Snorm => bytes.extend(endpoint_block([129, 127, 0, 64][variant])),
|
||||
BcChannelFormat::Bc5Snorm => {
|
||||
bytes.extend(endpoint_block([129, 127, 0, 64][variant]));
|
||||
bytes.extend(endpoint_block([127, 129, 64, 0][variant]));
|
||||
}
|
||||
},
|
||||
_ => unreachable!("native readback fixture covers BC1–BC5"),
|
||||
}
|
||||
}
|
||||
|
||||
fn expected_words(format: BcFormat, layer: usize, mip: usize) -> Vec<u32> {
|
||||
let (width, height) = EXTENTS[mip];
|
||||
let mut result = Vec::new();
|
||||
for y in 0..height {
|
||||
for x in 0..width {
|
||||
// The base image has a 3×2 physical block grid. Small levels
|
||||
// have a single block even when either texel extent is below 4.
|
||||
let variant = ((y / 4) * 3 + x / 4 + layer * 2 + mip) % 4;
|
||||
let rgba = match format {
|
||||
BcFormat::Bc1 | BcFormat::BcAlpha(_) => PIXELS[variant],
|
||||
BcFormat::BcChannel(BcChannelFormat::Bc4Unorm) => [PIXELS[variant][0], 0, 0, 255],
|
||||
BcFormat::BcChannel(BcChannelFormat::Bc5Unorm) => {
|
||||
[PIXELS[variant][0], PIXELS[variant][1], 0, 255]
|
||||
}
|
||||
BcFormat::BcChannel(BcChannelFormat::Bc4Snorm) => {
|
||||
[[129, 127, 0, 64][variant], 0, 0, 127]
|
||||
}
|
||||
BcFormat::BcChannel(BcChannelFormat::Bc5Snorm) => [
|
||||
[129, 127, 0, 64][variant],
|
||||
[127, 129, 64, 0][variant],
|
||||
0,
|
||||
127,
|
||||
],
|
||||
_ => unreachable!("native readback fixture covers BC1–BC5"),
|
||||
};
|
||||
result.push(u32::from_le_bytes(rgba));
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn read(
|
||||
gpu: &NativeCompute,
|
||||
upload: &PackedBcUpload<'_>,
|
||||
layer: usize,
|
||||
mip: u32,
|
||||
) -> Result<Vec<u32>, MetalError> {
|
||||
match upload.format() {
|
||||
BcFormat::Bc1 => gpu.read_bc1_upload(&upload.as_bc1(layer, mip).unwrap(), 0, 0),
|
||||
BcFormat::BcAlpha(_) => {
|
||||
gpu.read_bc_alpha_upload(&upload.as_bc_alpha(layer, mip).unwrap(), 0, 0)
|
||||
}
|
||||
BcFormat::BcChannel(_) => {
|
||||
gpu.read_bc_channel_upload(&upload.as_bc_channel(layer, mip).unwrap(), 0, 0)
|
||||
}
|
||||
_ => unreachable!("native readback fixture covers BC1–BC5"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn packed_and_aligned_bc_uploads_return_known_pixels_on_metal() {
|
||||
let gpu = NativeCompute::new().expect("native Metal device and queue");
|
||||
for format in [
|
||||
BcFormat::Bc1,
|
||||
BcFormat::BcAlpha(BcAlphaFormat::Bc2),
|
||||
BcFormat::BcAlpha(BcAlphaFormat::Bc3),
|
||||
BcFormat::BcChannel(BcChannelFormat::Bc4Unorm),
|
||||
BcFormat::BcChannel(BcChannelFormat::Bc4Snorm),
|
||||
BcFormat::BcChannel(BcChannelFormat::Bc5Unorm),
|
||||
BcFormat::BcChannel(BcChannelFormat::Bc5Snorm),
|
||||
] {
|
||||
let desc = BcTextureDesc {
|
||||
width: 9,
|
||||
height: 5,
|
||||
mips: 4,
|
||||
layers: 2,
|
||||
format,
|
||||
};
|
||||
let tight = BcTextureLayout::new(desc, BcCopyAlignment::TIGHT).unwrap();
|
||||
let aligned = BcTextureLayout::new(
|
||||
desc,
|
||||
BcCopyAlignment {
|
||||
row: 256,
|
||||
placement: 512,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let mut source = Vec::new();
|
||||
for layer in 0..2 {
|
||||
for (mip, block_count) in BLOCK_COUNTS.into_iter().enumerate() {
|
||||
for n in 0..block_count {
|
||||
append_block(&mut source, format, (n + layer * 2 + mip) % 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
let tight_upload = PackedBcUpload::new(&tight, &source).unwrap();
|
||||
let mut staged = vec![0xff; aligned.byte_len()];
|
||||
tight_upload.repack_into(&aligned, &mut staged).unwrap();
|
||||
let aligned_upload = PackedBcUpload::new(&aligned, &staged).unwrap();
|
||||
|
||||
for (name, upload) in [("tight", &tight_upload), ("aligned", &aligned_upload)] {
|
||||
for layer in 0..2 {
|
||||
for mip in 0..EXTENTS.len() {
|
||||
let actual = read(&gpu, upload, layer, mip as u32).unwrap_or_else(|error| {
|
||||
if error.is_capability_unavailable() {
|
||||
panic!("CAPABILITY UNAVAILABLE: {error}");
|
||||
}
|
||||
panic!("IMPLEMENTATION FAILURE: {format:?} {name} {layer}/{mip}: {error}");
|
||||
});
|
||||
assert_eq!(
|
||||
actual,
|
||||
expected_words(format, layer, mip),
|
||||
"{format:?} {name} layer {layer}, mip {mip}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
//! Linear copy footprints for independently authored block-compressed textures.
|
||||
//!
|
||||
//! For example, a 5×3 BC1 level has two 8-byte blocks forming one 16-byte row.
|
||||
//! Its 2×1 mip takes a single 8-byte block. In a tight layout the levels occupy
|
||||
//! 24 bytes; either can be borrowed directly from the same payload.
|
||||
//!
|
||||
//! ```
|
||||
//! use xenora_texture::{BcCopyAlignment, BcFormat, BcTextureDesc, BcTextureLayout, PackedBcUpload};
|
||||
//!
|
||||
//! let layout = BcTextureLayout::new(
|
||||
//! BcTextureDesc { width: 5, height: 3, mips: 2, layers: 1, format: BcFormat::Bc1 },
|
||||
//! BcCopyAlignment::TIGHT,
|
||||
//! )?;
|
||||
//! let mut bytes = vec![0; layout.byte_len()];
|
||||
//! layout.write_tight(&mut bytes, 0, 1, &[0x55; 8])?;
|
||||
//! let upload = PackedBcUpload::new(&layout, &bytes)?;
|
||||
//! assert_eq!(upload.subresource(0, 1)?.bytes, &[0x55; 8]);
|
||||
//! # Ok::<(), xenora_texture::TextureError>(())
|
||||
//! ```
|
||||
|
||||
use super::{
|
||||
BLOCK_BYTES, Bc1Subresource, Bc1Upload, Bc1UploadDesc, BcAlphaFormat, BcAlphaUpload,
|
||||
BcAlphaUploadDesc, BcChannelFormat, BcChannelUpload, BcChannelUploadDesc, TextureError,
|
||||
mip_blocks,
|
||||
};
|
||||
|
||||
/// Block encoding carried by the layout. The layout handles bytes without decoding texels.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum BcFormat {
|
||||
Bc1,
|
||||
BcAlpha(BcAlphaFormat),
|
||||
BcChannel(BcChannelFormat),
|
||||
Bc6hUfloat,
|
||||
Bc6hSfloat,
|
||||
Bc7,
|
||||
}
|
||||
|
||||
impl BcFormat {
|
||||
pub const fn block_bytes(self) -> usize {
|
||||
match self {
|
||||
Self::Bc1 => BLOCK_BYTES,
|
||||
Self::BcChannel(format) => format.block_bytes(),
|
||||
Self::BcAlpha(_) => BcAlphaUpload::BLOCK_BYTES,
|
||||
Self::Bc6hUfloat | Self::Bc6hSfloat | Self::Bc7 => 16,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Dimensions and encoding of a 2D texture or texture array.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct BcTextureDesc {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub mips: u32,
|
||||
pub layers: usize,
|
||||
pub format: BcFormat,
|
||||
}
|
||||
|
||||
/// Row and subresource placement alignments, in bytes. Each must be a nonzero
|
||||
/// power of two. Alignments smaller than a block are raised to its size.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct BcCopyAlignment {
|
||||
pub row: usize,
|
||||
pub placement: usize,
|
||||
}
|
||||
|
||||
impl BcCopyAlignment {
|
||||
pub const TIGHT: Self = Self {
|
||||
row: 1,
|
||||
placement: 1,
|
||||
};
|
||||
}
|
||||
|
||||
/// Logical dimensions and physical storage for one mip in one layer.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct BcFootprint {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub offset: usize,
|
||||
pub row_bytes: usize,
|
||||
pub row_pitch: usize,
|
||||
pub block_rows: usize,
|
||||
/// Full pitched rows, including the final row's padding.
|
||||
pub byte_len: usize,
|
||||
}
|
||||
|
||||
/// Layer-major storage with one footprint per mip. Layers share the same
|
||||
/// layout. Placement padding precedes each subresource as needed and there
|
||||
/// is no trailing placement padding at the end of the final layer.
|
||||
#[derive(Debug)]
|
||||
pub struct BcTextureLayout {
|
||||
desc: BcTextureDesc,
|
||||
mips: Vec<BcFootprint>,
|
||||
layer_stride: usize,
|
||||
byte_len: usize,
|
||||
}
|
||||
|
||||
impl BcTextureLayout {
|
||||
pub fn new(desc: BcTextureDesc, alignment: BcCopyAlignment) -> Result<Self, TextureError> {
|
||||
let max_mips = usize::BITS - desc.width.max(desc.height).leading_zeros();
|
||||
if desc.width == 0
|
||||
|| desc.height == 0
|
||||
|| desc.layers == 0
|
||||
|| desc.mips == 0
|
||||
|| desc.mips > max_mips
|
||||
|| !alignment.row.is_power_of_two()
|
||||
|| !alignment.placement.is_power_of_two()
|
||||
{
|
||||
return Err(TextureError::InvalidLayout);
|
||||
}
|
||||
|
||||
let block_bytes = desc.format.block_bytes();
|
||||
let row_alignment = alignment.row.max(block_bytes);
|
||||
let placement_alignment = alignment.placement.max(block_bytes);
|
||||
let mut mips = Vec::new();
|
||||
mips.try_reserve_exact(desc.mips as usize)
|
||||
.map_err(|_| TextureError::AllocationFailed)?;
|
||||
|
||||
let mut layer_len: usize = 0;
|
||||
for mip in 0..desc.mips {
|
||||
let (blocks_x, block_rows) = mip_blocks(desc.width, desc.height, mip);
|
||||
let row_bytes = blocks_x
|
||||
.checked_mul(block_bytes)
|
||||
.ok_or(TextureError::SizeOverflow)?;
|
||||
let row_pitch = align_up(row_bytes, row_alignment)?;
|
||||
let offset = align_up(layer_len, placement_alignment)?;
|
||||
let byte_len = row_pitch
|
||||
.checked_mul(block_rows)
|
||||
.ok_or(TextureError::SizeOverflow)?;
|
||||
layer_len = offset
|
||||
.checked_add(byte_len)
|
||||
.ok_or(TextureError::SizeOverflow)?;
|
||||
mips.push(BcFootprint {
|
||||
width: (desc.width >> mip).max(1),
|
||||
height: (desc.height >> mip).max(1),
|
||||
offset,
|
||||
row_bytes,
|
||||
row_pitch,
|
||||
block_rows,
|
||||
byte_len,
|
||||
});
|
||||
}
|
||||
|
||||
// A single layer never needs padding after its last mip.
|
||||
let layer_stride = if desc.layers == 1 {
|
||||
layer_len
|
||||
} else {
|
||||
align_up(layer_len, placement_alignment)?
|
||||
};
|
||||
let byte_len = layer_stride
|
||||
.checked_mul(desc.layers - 1)
|
||||
.and_then(|prefix| prefix.checked_add(layer_len))
|
||||
.ok_or(TextureError::SizeOverflow)?;
|
||||
Ok(Self {
|
||||
desc,
|
||||
mips,
|
||||
layer_stride,
|
||||
byte_len,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn byte_len(&self) -> usize {
|
||||
self.byte_len
|
||||
}
|
||||
|
||||
pub fn footprint(&self, layer: usize, mip: u32) -> Result<BcFootprint, TextureError> {
|
||||
if layer >= self.desc.layers {
|
||||
return Err(TextureError::OutOfBounds);
|
||||
}
|
||||
let footprint = self
|
||||
.mips
|
||||
.get(mip as usize)
|
||||
.ok_or(TextureError::OutOfBounds)?;
|
||||
Ok(self.at_layer(*footprint, layer))
|
||||
}
|
||||
|
||||
/// Writes tightly packed block rows to one subresource. Clears that
|
||||
/// subresource's row padding and leaves the rest of the payload alone.
|
||||
/// The destination must include exactly this layout's full byte span.
|
||||
pub fn write_tight(
|
||||
&self,
|
||||
dst: &mut [u8],
|
||||
layer: usize,
|
||||
mip: u32,
|
||||
src: &[u8],
|
||||
) -> Result<(), TextureError> {
|
||||
let fp = self.footprint(layer, mip)?;
|
||||
if dst.len() != self.byte_len || src.len() != fp.row_bytes * fp.block_rows {
|
||||
return Err(TextureError::InvalidByteLength);
|
||||
}
|
||||
|
||||
let dst = &mut dst[fp.offset..fp.offset + fp.byte_len];
|
||||
dst.fill(0);
|
||||
copy_rows(src, fp.row_bytes, dst, fp.row_pitch, fp.row_bytes);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn at_layer(&self, mut footprint: BcFootprint, layer: usize) -> BcFootprint {
|
||||
// Construction checked the span through the end of the final layer.
|
||||
footprint.offset += self.layer_stride * layer;
|
||||
footprint
|
||||
}
|
||||
}
|
||||
|
||||
/// A validated, borrowed payload. Padding bytes in its input may be arbitrary.
|
||||
#[derive(Debug)]
|
||||
pub struct PackedBcUpload<'a> {
|
||||
layout: &'a BcTextureLayout,
|
||||
bytes: &'a [u8],
|
||||
}
|
||||
|
||||
impl<'a> PackedBcUpload<'a> {
|
||||
pub fn new(layout: &'a BcTextureLayout, bytes: &'a [u8]) -> Result<Self, TextureError> {
|
||||
if bytes.len() != layout.byte_len {
|
||||
return Err(TextureError::InvalidByteLength);
|
||||
}
|
||||
Ok(Self { layout, bytes })
|
||||
}
|
||||
|
||||
pub fn format(&self) -> BcFormat {
|
||||
self.layout.desc.format
|
||||
}
|
||||
|
||||
/// Borrows the selected block rows, including their row padding.
|
||||
pub fn subresource(&self, layer: usize, mip: u32) -> Result<Bc1Subresource<'a>, TextureError> {
|
||||
let fp = self.layout.footprint(layer, mip)?;
|
||||
Ok(Bc1Subresource {
|
||||
bytes: &self.bytes[fp.offset..fp.offset + fp.byte_len],
|
||||
width: fp.width,
|
||||
height: fp.height,
|
||||
row_pitch: fp.row_pitch,
|
||||
})
|
||||
}
|
||||
|
||||
/// Copies every logical block to an equivalent layout and zeroes its padding.
|
||||
/// The source and destination must have the same dimensions, mip and layer
|
||||
/// counts, and block encoding. A rejected copy leaves the destination intact.
|
||||
pub fn repack_into(
|
||||
&self,
|
||||
layout: &BcTextureLayout,
|
||||
dst: &mut [u8],
|
||||
) -> Result<(), TextureError> {
|
||||
if self.layout.desc != layout.desc {
|
||||
return Err(TextureError::InvalidLayout);
|
||||
}
|
||||
if dst.len() != layout.byte_len {
|
||||
return Err(TextureError::InvalidByteLength);
|
||||
}
|
||||
|
||||
dst.fill(0);
|
||||
for layer in 0..layout.desc.layers {
|
||||
for (&source, &target) in self.layout.mips.iter().zip(&layout.mips) {
|
||||
let source = self.layout.at_layer(source, layer);
|
||||
let target = layout.at_layer(target, layer);
|
||||
copy_rows(
|
||||
&self.bytes[source.offset..source.offset + source.byte_len],
|
||||
source.row_pitch,
|
||||
&mut dst[target.offset..target.offset + target.byte_len],
|
||||
target.row_pitch,
|
||||
source.row_bytes,
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Adapts one BC1 subresource to the existing single-upload reader, without copying.
|
||||
pub fn as_bc1(&self, layer: usize, mip: u32) -> Result<Bc1Upload<'a>, TextureError> {
|
||||
if self.layout.desc.format != BcFormat::Bc1 {
|
||||
return Err(TextureError::InvalidLayout);
|
||||
}
|
||||
let view = self.subresource(layer, mip)?;
|
||||
Bc1Upload::new(
|
||||
view.bytes,
|
||||
Bc1UploadDesc {
|
||||
width: view.width,
|
||||
height: view.height,
|
||||
mips: 1,
|
||||
layers: 1,
|
||||
format: 1,
|
||||
usage: 1,
|
||||
row_pitch: view.row_pitch,
|
||||
slice_pitch: view.bytes.len(),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Adapts one BC2 or BC3 subresource to the existing reader, without copying.
|
||||
pub fn as_bc_alpha(&self, layer: usize, mip: u32) -> Result<BcAlphaUpload<'a>, TextureError> {
|
||||
let BcFormat::BcAlpha(format) = self.layout.desc.format else {
|
||||
return Err(TextureError::InvalidLayout);
|
||||
};
|
||||
let view = self.subresource(layer, mip)?;
|
||||
BcAlphaUpload::new(
|
||||
view.bytes,
|
||||
BcAlphaUploadDesc {
|
||||
width: view.width,
|
||||
height: view.height,
|
||||
mips: 1,
|
||||
layers: 1,
|
||||
format,
|
||||
row_pitch: view.row_pitch,
|
||||
slice_pitch: view.bytes.len(),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Adapts one BC4 or BC5 subresource to the existing reader, without copying.
|
||||
pub fn as_bc_channel(
|
||||
&self,
|
||||
layer: usize,
|
||||
mip: u32,
|
||||
) -> Result<BcChannelUpload<'a>, TextureError> {
|
||||
let BcFormat::BcChannel(format) = self.layout.desc.format else {
|
||||
return Err(TextureError::InvalidLayout);
|
||||
};
|
||||
let view = self.subresource(layer, mip)?;
|
||||
BcChannelUpload::new(
|
||||
view.bytes,
|
||||
BcChannelUploadDesc {
|
||||
width: view.width,
|
||||
height: view.height,
|
||||
mips: 1,
|
||||
layers: 1,
|
||||
format,
|
||||
row_pitch: view.row_pitch,
|
||||
slice_pitch: view.bytes.len(),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn align_up(value: usize, alignment: usize) -> Result<usize, TextureError> {
|
||||
value
|
||||
.checked_next_multiple_of(alignment)
|
||||
.ok_or(TextureError::SizeOverflow)
|
||||
}
|
||||
|
||||
fn copy_rows(src: &[u8], src_pitch: usize, dst: &mut [u8], dst_pitch: usize, row_bytes: usize) {
|
||||
for (src_row, dst_row) in src
|
||||
.chunks_exact(src_pitch)
|
||||
.zip(dst.chunks_exact_mut(dst_pitch))
|
||||
{
|
||||
dst_row[..row_bytes].copy_from_slice(&src_row[..row_bytes]);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
mod bc;
|
||||
mod bc6h;
|
||||
mod bc7;
|
||||
mod footprint;
|
||||
pub mod sampler;
|
||||
mod upload;
|
||||
pub use bc::{
|
||||
@@ -19,6 +20,9 @@ pub use bc6h::{
|
||||
BC6H_SUPPORTED_MODES, Bc6hError, bc6h_mode, decode_bc6h_sfloat, decode_bc6h_ufloat,
|
||||
};
|
||||
pub use bc7::{BC7_SUPPORTED_MODES, bc7_mode, decode_bc7};
|
||||
pub use footprint::{
|
||||
BcCopyAlignment, BcFootprint, BcFormat, BcTextureDesc, BcTextureLayout, PackedBcUpload,
|
||||
};
|
||||
pub use sampler::{AddressMode, FilterMode, SamplerDesc, SamplerError, TextureMetadata};
|
||||
pub use upload::{
|
||||
Bc1Subresource, Bc1Upload, Bc1UploadDesc, BcAlphaFormat, BcAlphaUpload, BcAlphaUploadDesc,
|
||||
|
||||
@@ -0,0 +1,375 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
use xenora_texture::{
|
||||
Bc1Upload, Bc1UploadDesc, BcAlphaFormat, BcChannelFormat, BcCopyAlignment, BcFootprint,
|
||||
BcFormat, BcTextureDesc, BcTextureLayout, PackedBcUpload, TextureError,
|
||||
};
|
||||
|
||||
const DESC: BcTextureDesc = BcTextureDesc {
|
||||
width: 13,
|
||||
height: 9,
|
||||
mips: 4,
|
||||
layers: 2,
|
||||
format: BcFormat::Bc1,
|
||||
};
|
||||
|
||||
const ALIGNED: BcCopyAlignment = BcCopyAlignment {
|
||||
row: 32,
|
||||
placement: 128,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn hand_calculated_footprints_cover_partial_blocks_and_layer_padding() {
|
||||
let tight = BcTextureLayout::new(DESC, BcCopyAlignment::TIGHT).unwrap();
|
||||
let aligned = BcTextureLayout::new(DESC, ALIGNED).unwrap();
|
||||
// 13×9 has four blocks across and three down. 6×4 takes two blocks.
|
||||
// Both 3×2 and 1×1 take one block each.
|
||||
let levels = [(13, 9, 32, 3), (6, 4, 16, 1), (3, 2, 8, 1), (1, 1, 8, 1)];
|
||||
for (layout, offsets, stride, pitches) in [
|
||||
(&tight, [0, 96, 112, 120], 128, [32, 16, 8, 8]),
|
||||
(&aligned, [0, 128, 256, 384], 512, [32; 4]),
|
||||
] {
|
||||
for layer in 0..2 {
|
||||
for (mip, (width, height, row_bytes, block_rows)) in levels.into_iter().enumerate() {
|
||||
let row_pitch = pitches[mip];
|
||||
assert_eq!(
|
||||
layout.footprint(layer, mip as u32),
|
||||
Ok(BcFootprint {
|
||||
width,
|
||||
height,
|
||||
offset: stride * layer + offsets[mip],
|
||||
row_bytes,
|
||||
row_pitch,
|
||||
block_rows,
|
||||
byte_len: row_pitch * block_rows,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(tight.byte_len(), 256);
|
||||
assert_eq!(aligned.byte_len(), 928);
|
||||
assert_eq!(aligned.footprint(2, 0), Err(TextureError::OutOfBounds));
|
||||
assert_eq!(aligned.footprint(0, 4), Err(TextureError::OutOfBounds));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compact_chain_uses_block_rows_instead_of_reserving_base_size_for_every_mip() {
|
||||
let desc = BcTextureDesc {
|
||||
width: 1024,
|
||||
height: 1024,
|
||||
mips: 11,
|
||||
layers: 1,
|
||||
..DESC
|
||||
};
|
||||
let layout = BcTextureLayout::new(desc, BcCopyAlignment::TIGHT).unwrap();
|
||||
assert_eq!(layout.byte_len(), 699_064);
|
||||
assert_eq!(layout.footprint(0, 10).unwrap().offset, 699_056);
|
||||
let compact = vec![0; layout.byte_len()];
|
||||
PackedBcUpload::new(&layout, &compact).unwrap();
|
||||
let fixed_slice = Bc1UploadDesc {
|
||||
width: 1024,
|
||||
height: 1024,
|
||||
mips: 11,
|
||||
layers: 1,
|
||||
format: 1,
|
||||
usage: 1,
|
||||
row_pitch: 2048,
|
||||
slice_pitch: 524_288,
|
||||
};
|
||||
assert_eq!(
|
||||
Bc1Upload::new(&compact, fixed_slice).unwrap_err(),
|
||||
TextureError::InvalidByteLength
|
||||
);
|
||||
let fixed = vec![0; 5_767_168];
|
||||
let fixed_upload = Bc1Upload::new(&fixed, fixed_slice).unwrap();
|
||||
assert_eq!(fixed_upload.subresource(0, 10).unwrap().bytes.len(), 2048);
|
||||
let aligned = BcTextureLayout::new(
|
||||
desc,
|
||||
BcCopyAlignment {
|
||||
row: 256,
|
||||
placement: 512,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(aligned.byte_len(), 705_280);
|
||||
assert_eq!(aligned.footprint(0, 10).unwrap().offset, 705_024);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_write_clears_its_row_padding_and_preserves_neighbors() {
|
||||
let layout = BcTextureLayout::new(BcTextureDesc { width: 9, ..DESC }, ALIGNED).unwrap();
|
||||
let src: Vec<u8> = (1..=72).collect(); // Three 24-byte rows.
|
||||
let mut dst = vec![0xcc; layout.byte_len()];
|
||||
layout.write_tight(&mut dst, 1, 0, &src).unwrap();
|
||||
let mut expected = vec![0xcc; dst.len()];
|
||||
expected[512..608].fill(0);
|
||||
expected[512..536].copy_from_slice(&src[..24]);
|
||||
expected[544..568].copy_from_slice(&src[24..48]);
|
||||
expected[576..600].copy_from_slice(&src[48..]);
|
||||
assert_eq!(dst, expected);
|
||||
let view = PackedBcUpload::new(&layout, &dst).unwrap();
|
||||
let level = view.subresource(1, 0).unwrap();
|
||||
assert_eq!(level.bytes, &dst[512..608]);
|
||||
assert!(std::ptr::eq(level.bytes.as_ptr(), dst[512..].as_ptr()));
|
||||
assert_eq!((level.width, level.height, level.row_pitch), (9, 9, 32));
|
||||
let legacy = view.as_bc1(1, 0).unwrap().subresource(0, 0).unwrap();
|
||||
assert!(std::ptr::eq(legacy.bytes.as_ptr(), level.bytes.as_ptr()));
|
||||
assert_eq!(legacy.bytes.len(), 96);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repack_ignores_dirty_source_padding_and_zeroes_both_kinds_of_target_padding() {
|
||||
let tight = BcTextureLayout::new(DESC, BcCopyAlignment::TIGHT).unwrap();
|
||||
let aligned = BcTextureLayout::new(DESC, ALIGNED).unwrap();
|
||||
let tight_bytes: Vec<u8> = (0..=255).collect();
|
||||
// Independent byte map from the paper footprints above.
|
||||
let spans = [
|
||||
(0, 0, 96),
|
||||
(96, 128, 16),
|
||||
(112, 256, 8),
|
||||
(120, 384, 8),
|
||||
(128, 512, 96),
|
||||
(224, 640, 16),
|
||||
(240, 768, 8),
|
||||
(248, 896, 8),
|
||||
];
|
||||
let mut dirty_aligned = vec![0xab; 928];
|
||||
let mut clean_aligned = vec![0; 928];
|
||||
for (src, dst, len) in spans {
|
||||
dirty_aligned[dst..dst + len].copy_from_slice(&tight_bytes[src..src + len]);
|
||||
clean_aligned[dst..dst + len].copy_from_slice(&tight_bytes[src..src + len]);
|
||||
}
|
||||
|
||||
let input = PackedBcUpload::new(&aligned, &dirty_aligned).unwrap();
|
||||
let mut compact = vec![0xcc; tight.byte_len()];
|
||||
input.repack_into(&tight, &mut compact).unwrap();
|
||||
assert_eq!(compact, tight_bytes);
|
||||
|
||||
let mut output = vec![0xcc; aligned.byte_len()];
|
||||
input.repack_into(&aligned, &mut output).unwrap();
|
||||
assert_eq!(output, clean_aligned);
|
||||
output.fill(0xcc);
|
||||
PackedBcUpload::new(&tight, &compact)
|
||||
.unwrap()
|
||||
.repack_into(&aligned, &mut output)
|
||||
.unwrap();
|
||||
assert_eq!(output, clean_aligned);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validates_sizes_before_writing_and_requires_the_same_encoding() {
|
||||
let layout = BcTextureLayout::new(DESC, ALIGNED).unwrap();
|
||||
let mut bytes = vec![0xab; layout.byte_len()];
|
||||
assert_eq!(
|
||||
layout.write_tight(&mut bytes, 0, 3, &[0; 7]),
|
||||
Err(TextureError::InvalidByteLength)
|
||||
);
|
||||
assert_eq!(
|
||||
layout.write_tight(&mut bytes, 0, 3, &[0; 9]),
|
||||
Err(TextureError::InvalidByteLength)
|
||||
);
|
||||
assert_eq!(
|
||||
layout.write_tight(&mut bytes[..927], 0, 3, &[0; 8]),
|
||||
Err(TextureError::InvalidByteLength)
|
||||
);
|
||||
assert_eq!(
|
||||
layout.write_tight(&mut bytes, 2, 3, &[0; 8]),
|
||||
Err(TextureError::OutOfBounds)
|
||||
);
|
||||
assert!(bytes.iter().all(|byte| *byte == 0xab));
|
||||
|
||||
for len in [0, 927, 929] {
|
||||
assert_eq!(
|
||||
PackedBcUpload::new(&layout, &vec![0; len]).unwrap_err(),
|
||||
TextureError::InvalidByteLength
|
||||
);
|
||||
}
|
||||
let upload = PackedBcUpload::new(&layout, &bytes).unwrap();
|
||||
for incompatible in [
|
||||
BcTextureDesc {
|
||||
format: BcFormat::BcChannel(BcChannelFormat::Bc4Unorm),
|
||||
..DESC
|
||||
},
|
||||
BcTextureDesc { width: 14, ..DESC },
|
||||
BcTextureDesc { height: 10, ..DESC },
|
||||
BcTextureDesc { mips: 3, ..DESC },
|
||||
BcTextureDesc { layers: 1, ..DESC },
|
||||
] {
|
||||
let target = BcTextureLayout::new(incompatible, ALIGNED).unwrap();
|
||||
let mut dst = vec![0xcc; target.byte_len()];
|
||||
assert_eq!(
|
||||
upload.repack_into(&target, &mut dst),
|
||||
Err(TextureError::InvalidLayout)
|
||||
);
|
||||
assert!(dst.iter().all(|byte| *byte == 0xcc));
|
||||
}
|
||||
let mut short = vec![0xcc; 927];
|
||||
assert_eq!(
|
||||
upload.repack_into(&layout, &mut short),
|
||||
Err(TextureError::InvalidByteLength)
|
||||
);
|
||||
assert!(short.iter().all(|byte| *byte == 0xcc));
|
||||
assert_eq!(
|
||||
upload.as_bc_alpha(0, 0).unwrap_err(),
|
||||
TextureError::InvalidLayout
|
||||
);
|
||||
assert_eq!(
|
||||
upload.as_bc_channel(0, 0).unwrap_err(),
|
||||
TextureError::InvalidLayout
|
||||
);
|
||||
assert_eq!(upload.as_bc1(0, 4).unwrap_err(), TextureError::OutOfBounds);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_descriptors_and_overflowing_footprints() {
|
||||
for desc in [
|
||||
BcTextureDesc { width: 0, ..DESC },
|
||||
BcTextureDesc { height: 0, ..DESC },
|
||||
BcTextureDesc { mips: 0, ..DESC },
|
||||
BcTextureDesc { mips: 5, ..DESC },
|
||||
BcTextureDesc { layers: 0, ..DESC },
|
||||
] {
|
||||
assert_eq!(
|
||||
BcTextureLayout::new(desc, ALIGNED).unwrap_err(),
|
||||
TextureError::InvalidLayout
|
||||
);
|
||||
}
|
||||
for (row, placement) in [(0, 1), (1, 0), (3, 1), (1, 3)] {
|
||||
assert_eq!(
|
||||
BcTextureLayout::new(DESC, BcCopyAlignment { row, placement }).unwrap_err(),
|
||||
TextureError::InvalidLayout
|
||||
);
|
||||
}
|
||||
for desc in [
|
||||
BcTextureDesc {
|
||||
width: usize::MAX,
|
||||
..DESC
|
||||
},
|
||||
BcTextureDesc {
|
||||
height: usize::MAX,
|
||||
..DESC
|
||||
},
|
||||
BcTextureDesc {
|
||||
layers: usize::MAX,
|
||||
..DESC
|
||||
},
|
||||
] {
|
||||
assert_eq!(
|
||||
BcTextureLayout::new(desc, ALIGNED).unwrap_err(),
|
||||
TextureError::SizeOverflow
|
||||
);
|
||||
}
|
||||
let highest_bit = 1 << (usize::BITS - 1);
|
||||
for alignment in [
|
||||
BcCopyAlignment {
|
||||
row: highest_bit,
|
||||
placement: 1,
|
||||
},
|
||||
BcCopyAlignment {
|
||||
row: 1,
|
||||
placement: highest_bit,
|
||||
},
|
||||
] {
|
||||
assert_eq!(
|
||||
BcTextureLayout::new(DESC, alignment).unwrap_err(),
|
||||
TextureError::SizeOverflow
|
||||
);
|
||||
}
|
||||
// Even an unusually large placement alignment needs no trailing space
|
||||
// after a sole subresource.
|
||||
let one = BcTextureDesc {
|
||||
mips: 1,
|
||||
layers: 1,
|
||||
..DESC
|
||||
};
|
||||
let layout = BcTextureLayout::new(
|
||||
one,
|
||||
BcCopyAlignment {
|
||||
row: 1,
|
||||
placement: highest_bit,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(layout.byte_len(), 96);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_block_sizes_match_a_byte_by_byte_layout_oracle() {
|
||||
let formats = [
|
||||
(BcFormat::Bc1, 8),
|
||||
(BcFormat::BcAlpha(BcAlphaFormat::Bc2), 16),
|
||||
(BcFormat::BcAlpha(BcAlphaFormat::Bc3), 16),
|
||||
(BcFormat::BcChannel(BcChannelFormat::Bc4Unorm), 8),
|
||||
(BcFormat::BcChannel(BcChannelFormat::Bc4Snorm), 8),
|
||||
(BcFormat::BcChannel(BcChannelFormat::Bc5Unorm), 16),
|
||||
(BcFormat::BcChannel(BcChannelFormat::Bc5Snorm), 16),
|
||||
(BcFormat::Bc6hUfloat, 16),
|
||||
(BcFormat::Bc6hSfloat, 16),
|
||||
(BcFormat::Bc7, 16),
|
||||
];
|
||||
for (format, block_bytes) in formats {
|
||||
for (width, height) in [
|
||||
(1_usize, 1_usize),
|
||||
(1, 37),
|
||||
(37, 1),
|
||||
(4, 4),
|
||||
(5, 9),
|
||||
(17, 13),
|
||||
] {
|
||||
for (row, placement) in [(1, 1), (32, 16), (16, 128), (64, 512)] {
|
||||
let mips = usize::BITS - width.max(height).leading_zeros();
|
||||
let desc = BcTextureDesc {
|
||||
width,
|
||||
height,
|
||||
mips,
|
||||
layers: 3,
|
||||
format,
|
||||
};
|
||||
let layout =
|
||||
BcTextureLayout::new(desc, BcCopyAlignment { row, placement }).unwrap();
|
||||
let tight = BcTextureLayout::new(desc, BcCopyAlignment::TIGHT).unwrap();
|
||||
let mut expected = Vec::new();
|
||||
let mut source = Vec::new();
|
||||
for layer in 0..3 {
|
||||
let (mut w, mut h) = (width, height);
|
||||
for mip in 0..mips {
|
||||
while expected.len() % placement != 0 {
|
||||
expected.push(0);
|
||||
}
|
||||
let start = expected.len();
|
||||
let mut tight_level = Vec::new();
|
||||
for y in (0..h).step_by(4) {
|
||||
let row_start = expected.len();
|
||||
for x in (0..w).step_by(4) {
|
||||
for i in 0..block_bytes {
|
||||
let byte =
|
||||
(1 + layer * 51 + mip as usize * 7 + x + 3 * y + i) as u8;
|
||||
expected.push(byte);
|
||||
tight_level.push(byte);
|
||||
}
|
||||
}
|
||||
while (expected.len() - row_start) % row != 0 {
|
||||
expected.push(0);
|
||||
}
|
||||
}
|
||||
let fp = layout.footprint(layer, mip).unwrap();
|
||||
assert_eq!(
|
||||
(fp.offset, fp.offset + fp.byte_len),
|
||||
(start, expected.len())
|
||||
);
|
||||
source.extend(tight_level);
|
||||
(w, h) = ((w / 2).max(1), (h / 2).max(1));
|
||||
}
|
||||
}
|
||||
let upload = PackedBcUpload::new(&tight, &source).unwrap();
|
||||
let mut bytes = vec![0xff; layout.byte_len()];
|
||||
upload.repack_into(&layout, &mut bytes).unwrap();
|
||||
assert_eq!(
|
||||
bytes, expected,
|
||||
"{format:?}, {width}×{height}, {row}/{placement}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user