mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 11:00:43 +00:00
Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09cf27186c | ||
|
|
9ac1343e34 | ||
|
|
078afd4591 | ||
|
|
4741409de1 | ||
|
|
6cbe976990 | ||
|
|
857bec29e1 | ||
|
|
7b6c6fd92c | ||
|
|
28846561ab |
No files matched your search
@@ -11,7 +11,9 @@ remains unsupported.
|
||||
|
||||
### Added
|
||||
|
||||
- Added a native Metal blend pass, `NativeCompute::render_blend`: up to 16 flat-coloured triangles drawn in order into an RGBA8 target, each with its own colour write mask (any subset of R, G, B, A) and either blending disabled or separate RGB and alpha blend factors (Zero, One, source and destination colour and alpha, and their one-minus forms) and operations (Add, Subtract, ReverseSubtract, and Min and Max, which take the unweighted values). Checked on the GPU with Metal validation in 39 passes over two original overlapping triangles: a clear-only pass; blending disabled; each source factor with the destination term zeroed and each destination factor with the source term zeroed; every operation; additive clamping; separate alpha factors; a separate alpha operation; eight write masks (each channel alone, RGB, none, RGBA, and RG with blending); and a pass whose first draw is itself blended and masked. Colours are compared per channel against images derived independently (in exact fractions) from the public Metal blend equations, which a new CPU reference in `xenora-ref` also reproduces. The tolerance was fixed before measuring: channels whose exact result is a whole 8-bit value must match exactly, and channels that need rounding may differ by 1. On an Apple M5 Pro every rounded channel matched round-to-nearest exactly. Synthetic fixtures only; no console blend state is involved.
|
||||
- Added indexed triangle draws. Native Metal: `NativeCompute::render_indexed` issues one real `drawIndexedPrimitives` call per draw (up to 16) on a 16- or 32-bit index buffer with a start index and a signed base vertex, into an RGBA8 target; a draw can sample an original BC1 texture uploaded natively. Before encoding anything it rejects the whole pass for a bad index count, an index range past the buffer, any `index + base vertex` outside the vertex buffer, and, as this API's policy, a raw all-ones index (the primitive-restart value, which Metal does not define for triangle lists). Loader: a synthetic, CPU-side index buffer binding (resource handle and offset, the view running to the end of the resource) with `cmd_set_index_buffer` and `cmd_draw_indexed_instanced`, which fetch one triangle, drawn once, through the index buffer, start index and base vertex and pass it to the existing one-triangle backend; each drawn triangle replaces the previous output. Recording rejects formats other than DXGI `R16_UINT` and `R32_UINT`, offsets not aligned to the index size or past the resource, and unknown resources. Execution checks each draw in turn, so earlier draws have already rendered when a later one fails: index counts other than 0 or 3, more than one instance or a nonzero start instance, a missing index buffer, indices past the buffer and an `index + base vertex` outside the vertex buffer are typed errors; a valid zero index or instance count draws nothing. All-ones indices are ordinary indices there. Checked on the GPU with Metal validation in 16 passes (both index sizes, 32-bit indices above 16 bits, odd 16-bit start, positive and negative base vertex, reversed winding, flat and textured two-triangle draws, draw order, BC1-textured draws), compared per pixel against images derived independently from the geometry, the public D3D12 and Metal index semantics and the public BC1 rules, which a new CPU reference in `xenora-ref` also reproduces; the loader's fetch is checked against literal positions with synthetic guest-backed buffers and through the native Metal renderer. Synthetic fixtures only; no console index-buffer views, guest programs or console draw state are involved.
|
||||
- Added a `cpu_decode` cargo-fuzz target for the static x86-64 decoder used by the read-only PE probe. It places up to 4,096 fuzz bytes as the code section of a fixed, original PE32+ header and runs `probe_pe_bytes` twice with a bounded `ProbeConfig` (4,096 bytes, 2,048 instructions, 1,024 worklist entries); nothing is executed. It checks that the two reports are identical and that every decoded instruction is 1 to 15 bytes long, lies inside the code and matches the input bytes. A startup self-check aborts the run if the synthetic header stops reaching the decoder. A 600-second local campaign (nightly cargo-fuzz, one fork-mode worker, seeded from instruction bytes in the loader's probe tests; about 2.38 million executions, coverage 1,350 edges) found one decoder defect, reported to the CPU owner on #74 and not fixed here: the probe accepts unlimited FS/GS segment prefixes and reports instructions longer than 15 bytes (minimized input: 16 bytes of `0x65`). Run it with `rustup run nightly cargo fuzz run cpu_decode`; `scripts/fuzz.sh` does not list it yet.
|
||||
- Added a `pe_parse` cargo-fuzz target that feeds malformed bytes to the PE32+ parser (`ParsedPe::parse`, `rva_to_file_offset`) and, only when the summed section mapping size and `SizeOfImage` are both at most 64 MiB, to the checked guest-memory mapping step (`load_into_memory`); nothing is executed. A 600-second local campaign (nightly cargo-fuzz, fork mode, seeded from the repository's synthetic PE fixtures, about 120,000 executions, coverage 267 edges) found two loader panics, reported to the CPU owner and not fixed here: truncated optional headers panic on out-of-range slices in `parse`, and `rva_to_file_offset` overflows `u32` when a section's address plus size exceeds 4 GiB. Run it with `rustup run nightly cargo fuzz run pe_parse`; `scripts/fuzz.sh` does not list it yet.
|
||||
- Added `NativeCompute::run_words_with_group_width` and `max_group_width` to run a trusted kernel at an explicit threadgroup width, rejecting zero and widths above the device or pipeline limit. Native tests check 91 legal width and grid-size pairs (widths 1 to 1,024, including non-divisible grids) against independently computed outputs, and confirm the dispatched group sizes, including trimmed last groups. On an Apple M5 Pro, in three release runs of a 4,096-thread integer-mixing kernel (30 samples per width), widths 64, 128 and 256 were within 0.3 % of each other in two runs, at a median of 58.8 µs of GPU time. Width 512 was 5 to 6 % slower and width 1,024 about twice as slow in all three runs. The first run was about 4.7 times slower overall for reasons not determined, and in it 128 and 256 were 3 to 4 % faster than 64. The default width of 64 is unchanged. This was measured on one device and one workload.
|
||||
- Added a native Metal depth pass, `NativeCompute::render_depth`: up to 16 triangles drawn in order into an RGBA8 target with an optional `Depth32Float` attachment, a chosen clear depth, a per-draw depth compare function (all eight) and depth write enable. A pass without an attachment accepts only `DepthState::DISABLED`. Colour and the stored depth are both read back; the depth values are returned as the texture read gives them, without numeric conversion. A draw can sample an original BC1 texture whose blocks are uploaded to a native `BC1_RGBA` texture without CPU decoding, gated on `supportsBCTextureCompression`. Checked on the GPU with Metal validation in 30 passes of original overlapping triangles: clear, every compare function at three depths, write enabled and disabled, depth test disabled (`Always` without writes) with an attachment and no attachment at all, and a BC1-textured draw. Colour and depth are compared per pixel against expectations derived independently from the geometry, the Metal compare semantics and the public BC1 rules, which a new CPU reference in `xenora-ref` also reproduces. These are synthetic fixtures only; no console depth formats or guest render state are involved.
|
||||
|
||||
@@ -24,8 +24,9 @@ use xenora_texture::{
|
||||
mod render;
|
||||
|
||||
pub use render::{
|
||||
DepthCompare, DepthDraw, DepthFill, DepthOutput, DepthPass, DepthState, IndexBuffer,
|
||||
IndexedDraw, IndexedPass,
|
||||
BlendDraw, BlendFactor, BlendOperation, BlendPass, BlendState, ColorWriteMask, DepthCompare,
|
||||
DepthDraw, DepthFill, DepthOutput, DepthPass, DepthState, IndexBuffer, IndexedDraw,
|
||||
IndexedPass,
|
||||
};
|
||||
|
||||
const MAX_THREADS_PER_GROUP: usize = 64;
|
||||
|
||||
@@ -14,8 +14,9 @@ pub mod texture_pipeline;
|
||||
|
||||
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
|
||||
pub use compute::{
|
||||
DepthCompare, DepthDraw, DepthFill, DepthOutput, DepthPass, DepthState, GuestBuffer,
|
||||
GuestBufferView, IndexBuffer, IndexedDraw, IndexedPass, MetalError, NativeCompute, RunProfile,
|
||||
BlendDraw, BlendFactor, BlendOperation, BlendPass, BlendState, ColorWriteMask, DepthCompare,
|
||||
DepthDraw, DepthFill, DepthOutput, DepthPass, DepthState, GuestBuffer, GuestBufferView,
|
||||
IndexBuffer, IndexedDraw, IndexedPass, MetalError, NativeCompute, RunProfile,
|
||||
};
|
||||
|
||||
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
|
||||
|
||||
+288
-6
@@ -6,12 +6,12 @@ use objc2::rc::{Retained, autoreleasepool};
|
||||
use objc2::runtime::{NSObjectProtocol, ProtocolObject};
|
||||
use objc2::sel;
|
||||
use objc2_metal::{
|
||||
MTLClearColor, MTLCommandBuffer, MTLCommandEncoder, MTLCommandQueue, MTLCompareFunction,
|
||||
MTLDepthStencilDescriptor, MTLDepthStencilState, MTLDevice, MTLFunction, MTLIndexType,
|
||||
MTLLoadAction, MTLOrigin, MTLPixelFormat, MTLPrimitiveType, MTLRegion, MTLRenderCommandEncoder,
|
||||
MTLRenderPassDescriptor, MTLRenderPipelineDescriptor, MTLRenderPipelineState,
|
||||
MTLShaderValidation, MTLSize, MTLStorageMode, MTLStoreAction, MTLTexture, MTLTextureDescriptor,
|
||||
MTLTextureUsage, MTLViewport,
|
||||
MTLBlendFactor, MTLBlendOperation, MTLClearColor, MTLColorWriteMask, MTLCommandBuffer,
|
||||
MTLCommandEncoder, MTLCommandQueue, MTLCompareFunction, MTLDepthStencilDescriptor,
|
||||
MTLDepthStencilState, MTLDevice, MTLFunction, MTLIndexType, MTLLoadAction, MTLOrigin,
|
||||
MTLPixelFormat, MTLPrimitiveType, MTLRegion, MTLRenderCommandEncoder, MTLRenderPassDescriptor,
|
||||
MTLRenderPipelineDescriptor, MTLRenderPipelineState, MTLShaderValidation, MTLSize,
|
||||
MTLStorageMode, MTLStoreAction, MTLTexture, MTLTextureDescriptor, MTLTextureUsage, MTLViewport,
|
||||
};
|
||||
|
||||
use xenora_texture::{Bc1Subresource, Bc1Upload};
|
||||
@@ -1081,3 +1081,285 @@ fn whole(width: usize, height: usize) -> MTLRegion {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// A blend factor, as Metal defines it for the RGB and the alpha channel.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum BlendFactor {
|
||||
Zero,
|
||||
One,
|
||||
SourceColor,
|
||||
OneMinusSourceColor,
|
||||
SourceAlpha,
|
||||
OneMinusSourceAlpha,
|
||||
DestinationColor,
|
||||
OneMinusDestinationColor,
|
||||
DestinationAlpha,
|
||||
OneMinusDestinationAlpha,
|
||||
}
|
||||
|
||||
impl BlendFactor {
|
||||
fn metal(self) -> MTLBlendFactor {
|
||||
match self {
|
||||
Self::Zero => MTLBlendFactor::Zero,
|
||||
Self::One => MTLBlendFactor::One,
|
||||
Self::SourceColor => MTLBlendFactor::SourceColor,
|
||||
Self::OneMinusSourceColor => MTLBlendFactor::OneMinusSourceColor,
|
||||
Self::SourceAlpha => MTLBlendFactor::SourceAlpha,
|
||||
Self::OneMinusSourceAlpha => MTLBlendFactor::OneMinusSourceAlpha,
|
||||
Self::DestinationColor => MTLBlendFactor::DestinationColor,
|
||||
Self::OneMinusDestinationColor => MTLBlendFactor::OneMinusDestinationColor,
|
||||
Self::DestinationAlpha => MTLBlendFactor::DestinationAlpha,
|
||||
Self::OneMinusDestinationAlpha => MTLBlendFactor::OneMinusDestinationAlpha,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A blend operation combining the weighted source and destination; `Min`
|
||||
/// and `Max` take the unweighted values and ignore the factors.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum BlendOperation {
|
||||
Add,
|
||||
Subtract,
|
||||
ReverseSubtract,
|
||||
Min,
|
||||
Max,
|
||||
}
|
||||
|
||||
impl BlendOperation {
|
||||
fn metal(self) -> MTLBlendOperation {
|
||||
match self {
|
||||
Self::Add => MTLBlendOperation::Add,
|
||||
Self::Subtract => MTLBlendOperation::Subtract,
|
||||
Self::ReverseSubtract => MTLBlendOperation::ReverseSubtract,
|
||||
Self::Min => MTLBlendOperation::Min,
|
||||
Self::Max => MTLBlendOperation::Max,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Separate RGB and alpha blend equations of one draw.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct BlendState {
|
||||
pub source_rgb: BlendFactor,
|
||||
pub destination_rgb: BlendFactor,
|
||||
pub rgb_operation: BlendOperation,
|
||||
pub source_alpha: BlendFactor,
|
||||
pub destination_alpha: BlendFactor,
|
||||
pub alpha_operation: BlendOperation,
|
||||
}
|
||||
|
||||
/// The colour channels a draw writes; the others keep the stored value.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct ColorWriteMask {
|
||||
pub red: bool,
|
||||
pub green: bool,
|
||||
pub blue: bool,
|
||||
pub alpha: bool,
|
||||
}
|
||||
|
||||
impl ColorWriteMask {
|
||||
pub const ALL: Self = Self {
|
||||
red: true,
|
||||
green: true,
|
||||
blue: true,
|
||||
alpha: true,
|
||||
};
|
||||
|
||||
fn metal(self) -> MTLColorWriteMask {
|
||||
[
|
||||
(self.red, MTLColorWriteMask::Red),
|
||||
(self.green, MTLColorWriteMask::Green),
|
||||
(self.blue, MTLColorWriteMask::Blue),
|
||||
(self.alpha, MTLColorWriteMask::Alpha),
|
||||
]
|
||||
.into_iter()
|
||||
.filter(|(on, _)| *on)
|
||||
.fold(MTLColorWriteMask::None, |mask, (_, bit)| mask | bit)
|
||||
}
|
||||
}
|
||||
|
||||
/// One flat-coloured triangle of a blend pass.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct BlendDraw {
|
||||
pub positions: [[f32; 4]; 3],
|
||||
pub color: [u8; 4],
|
||||
/// `None` disables blending: the source replaces the masked channels.
|
||||
pub blend: Option<BlendState>,
|
||||
pub write_mask: ColorWriteMask,
|
||||
}
|
||||
|
||||
/// An offscreen pass of up to 16 triangles drawn in order into a cleared
|
||||
/// RGBA8Unorm target without depth attachment.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct BlendPass<'a> {
|
||||
pub extent: [usize; 2],
|
||||
pub clear_color: [u8; 4],
|
||||
pub draws: &'a [BlendDraw],
|
||||
}
|
||||
|
||||
impl NativeCompute {
|
||||
/// Draw a blended, write-masked offscreen pass and read back its colour.
|
||||
///
|
||||
/// Blend state and write mask are pipeline state in Metal, so each draw
|
||||
/// gets its own pipeline built from exactly the draw's settings.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error for an invalid extent, too many draws, non-finite
|
||||
/// positions, or Metal setup or execution failure.
|
||||
pub fn render_blend(&self, pass: &BlendPass<'_>) -> Result<Vec<u8>, MetalError> {
|
||||
let [width, height] = pass.extent;
|
||||
let pixels = width
|
||||
.checked_mul(height)
|
||||
.filter(|&count| (1..=MAX_PIXELS).contains(&count))
|
||||
.ok_or_else(|| MetalError::new("render bounds", "expected 1..=4096 pixels"))?;
|
||||
if pass.draws.len() > MAX_DEPTH_DRAWS {
|
||||
return Err(MetalError::new("blend pass", "expected at most 16 draws"));
|
||||
}
|
||||
if pass
|
||||
.draws
|
||||
.iter()
|
||||
.flat_map(|draw| draw.positions.as_flattened())
|
||||
.any(|value| !value.is_finite())
|
||||
{
|
||||
return Err(MetalError::new(
|
||||
"render input",
|
||||
"expected finite vertex data",
|
||||
));
|
||||
}
|
||||
|
||||
autoreleasepool(|_| {
|
||||
let options = compile_options()?;
|
||||
let library = self.compile_library(DEPTH_SOURCE, &options)?;
|
||||
let vertex = find_function(&library, "depth_vertex")?;
|
||||
let fragment = find_function(&library, "depth_flat")?;
|
||||
let pipelines = pass
|
||||
.draws
|
||||
.iter()
|
||||
.map(|draw| self.blend_pipeline(&vertex, &fragment, draw))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let color = self.new_target(
|
||||
PIXEL_FORMAT,
|
||||
width,
|
||||
height,
|
||||
MTLStorageMode::Shared,
|
||||
MTLTextureUsage::RenderTarget,
|
||||
)?;
|
||||
let pass_descriptor = MTLRenderPassDescriptor::renderPassDescriptor();
|
||||
// SAFETY: Slot zero is the only color attachment for this pass.
|
||||
let attachment = unsafe {
|
||||
pass_descriptor
|
||||
.colorAttachments()
|
||||
.objectAtIndexedSubscript(0)
|
||||
};
|
||||
attachment.setTexture(Some(&color));
|
||||
attachment.setLoadAction(MTLLoadAction::Clear);
|
||||
let [red, green, blue, alpha] = pass.clear_color.map(|value| f64::from(value) / 255.0);
|
||||
attachment.setClearColor(MTLClearColor {
|
||||
red,
|
||||
green,
|
||||
blue,
|
||||
alpha,
|
||||
});
|
||||
attachment.setStoreAction(MTLStoreAction::Store);
|
||||
let words: Vec<u32> = pass
|
||||
.draws
|
||||
.iter()
|
||||
.flat_map(|draw| draw.positions.as_flattened())
|
||||
.map(|value| value.to_bits())
|
||||
.collect();
|
||||
let positions = (!words.is_empty())
|
||||
.then(|| self.new_buffer(&words))
|
||||
.transpose()?;
|
||||
let command = self.queue.commandBuffer().ok_or_else(|| {
|
||||
MetalError::new("commandBuffer", "render command buffer creation failed")
|
||||
})?;
|
||||
let encoder = command
|
||||
.renderCommandEncoderWithDescriptor(&pass_descriptor)
|
||||
.ok_or_else(|| {
|
||||
MetalError::new(
|
||||
"renderCommandEncoderWithDescriptor",
|
||||
"render encoder creation failed",
|
||||
)
|
||||
})?;
|
||||
encoder.setViewport(MTLViewport {
|
||||
originX: 0.0,
|
||||
originY: 0.0,
|
||||
width: width as f64,
|
||||
height: height as f64,
|
||||
znear: 0.0,
|
||||
zfar: 1.0,
|
||||
});
|
||||
for (index, (draw, pipeline)) in pass.draws.iter().zip(&pipelines).enumerate() {
|
||||
encoder.setRenderPipelineState(pipeline);
|
||||
let rgba = draw.color.map(|value| f32::from(value) / 255.0);
|
||||
// SAFETY: The position buffer holds three float4s per draw and
|
||||
// the vertex stage reads only this draw's three. The fragment
|
||||
// constant is copied by Metal and matches the declared float4.
|
||||
unsafe {
|
||||
encoder.setVertexBuffer_offset_atIndex(positions.as_deref(), index * 48, 0);
|
||||
encoder.setFragmentBytes_length_atIndex(
|
||||
NonNull::from(&rgba).cast(),
|
||||
size_of_val(&rgba),
|
||||
0,
|
||||
);
|
||||
encoder.drawPrimitives_vertexStart_vertexCount(
|
||||
MTLPrimitiveType::Triangle,
|
||||
0,
|
||||
3,
|
||||
);
|
||||
}
|
||||
}
|
||||
encoder.endEncoding();
|
||||
complete_command(&command)?;
|
||||
|
||||
let mut output = vec![0; pixels * 4];
|
||||
// SAFETY: GPU writes have completed, and the initialized output
|
||||
// allocation fits the entire shared texture with tightly packed rows.
|
||||
unsafe {
|
||||
color.getBytes_bytesPerRow_fromRegion_mipmapLevel(
|
||||
NonNull::from(output.as_mut_slice()).cast(),
|
||||
width * 4,
|
||||
whole(width, height),
|
||||
0,
|
||||
);
|
||||
}
|
||||
Ok(output)
|
||||
})
|
||||
}
|
||||
|
||||
fn blend_pipeline(
|
||||
&self,
|
||||
vertex: &ProtocolObject<dyn MTLFunction>,
|
||||
fragment: &ProtocolObject<dyn MTLFunction>,
|
||||
draw: &BlendDraw,
|
||||
) -> Result<Retained<ProtocolObject<dyn MTLRenderPipelineState>>, MetalError> {
|
||||
let descriptor = MTLRenderPipelineDescriptor::new();
|
||||
descriptor.setVertexFunction(Some(vertex));
|
||||
descriptor.setFragmentFunction(Some(fragment));
|
||||
// SAFETY: Slot zero is the only color attachment for this pipeline.
|
||||
let attachment = unsafe { descriptor.colorAttachments().objectAtIndexedSubscript(0) };
|
||||
attachment.setPixelFormat(PIXEL_FORMAT);
|
||||
attachment.setWriteMask(draw.write_mask.metal());
|
||||
attachment.setBlendingEnabled(draw.blend.is_some());
|
||||
if let Some(blend) = draw.blend {
|
||||
attachment.setSourceRGBBlendFactor(blend.source_rgb.metal());
|
||||
attachment.setDestinationRGBBlendFactor(blend.destination_rgb.metal());
|
||||
attachment.setRgbBlendOperation(blend.rgb_operation.metal());
|
||||
attachment.setSourceAlphaBlendFactor(blend.source_alpha.metal());
|
||||
attachment.setDestinationAlphaBlendFactor(blend.destination_alpha.metal());
|
||||
attachment.setAlphaBlendOperation(blend.alpha_operation.metal());
|
||||
}
|
||||
let pipeline = self
|
||||
.device
|
||||
.newRenderPipelineStateWithDescriptor_error(&descriptor)
|
||||
.map_err(|error| {
|
||||
MetalError::new(
|
||||
"newRenderPipelineStateWithDescriptor",
|
||||
error.localizedDescription().to_string(),
|
||||
)
|
||||
})?;
|
||||
verify_render_validation(&pipeline)?;
|
||||
Ok(pipeline)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
//! CPU check: the blend reference agrees with the independently derived
|
||||
//! expectations for every blend case, colour and rounding flags alike. Runs
|
||||
//! without a GPU.
|
||||
|
||||
#[path = "support/blend_cases.rs"]
|
||||
mod blend_cases;
|
||||
|
||||
use blend_cases::{CLEAR, SIZE, cases, expected};
|
||||
use xenora_ref::blend::{RefBlendDraw, render_blend};
|
||||
|
||||
#[test]
|
||||
fn reference_matches_independent_expectations() {
|
||||
let (cases, expected) = (cases(), expected());
|
||||
assert_eq!(cases.len(), 39);
|
||||
assert_eq!(expected.len(), cases.len());
|
||||
for (case, want) in cases.iter().zip(&expected) {
|
||||
assert_eq!(case.name, want.name);
|
||||
let draws: Vec<_> = case
|
||||
.draws
|
||||
.iter()
|
||||
.map(|draw| RefBlendDraw {
|
||||
positions: draw.positions,
|
||||
color: draw.color,
|
||||
blend: draw.blend,
|
||||
mask: draw.mask,
|
||||
})
|
||||
.collect();
|
||||
let pixels = render_blend([SIZE, SIZE], CLEAR, &draws).expect("reference render");
|
||||
let color: Vec<_> = pixels.iter().map(|p| p.map(|c| c.value)).collect();
|
||||
let exact: Vec<_> = pixels.iter().map(|p| p.map(|c| c.exact)).collect();
|
||||
assert_eq!(color, want.color, "{} colour", case.name);
|
||||
assert_eq!(exact, want.exact, "{} rounding flags", case.name);
|
||||
}
|
||||
}
|
||||
+842
@@ -0,0 +1,842 @@
|
||||
case clear
|
||||
palette A 32 64 96 128 eeee
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
color AAAAAAAAAAAAAAAA
|
||||
end
|
||||
case disabled
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 60 180 240 96 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCCCCCCC
|
||||
color AAAACCCCCCCCCCCB
|
||||
color AAAAACCCCCCCCCBB
|
||||
color AAAAACCCCCCCCBBB
|
||||
color AAAAACCCCCCCBBBB
|
||||
color AAABBCCCCCCBBBBB
|
||||
color AABBBCCCCCCBBBBB
|
||||
color ABBBBCCCCCBBBBBB
|
||||
color BBBBBCCCCBBBBBBB
|
||||
color BBBBBBCCBBBBBBBB
|
||||
color BBBBBBCBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case src_Zero
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 0 0 0 0 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCCCCCCC
|
||||
color AAAACCCCCCCCCCCB
|
||||
color AAAAACCCCCCCCCBB
|
||||
color AAAAACCCCCCCCBBB
|
||||
color AAAAACCCCCCCBBBB
|
||||
color AAABBCCCCCCBBBBB
|
||||
color AABBBCCCCCCBBBBB
|
||||
color ABBBBCCCCCBBBBBB
|
||||
color BBBBBCCCCBBBBBBB
|
||||
color BBBBBBCCBBBBBBBB
|
||||
color BBBBBBCBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case src_One
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 60 180 240 96 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCCCCCCC
|
||||
color AAAACCCCCCCCCCCB
|
||||
color AAAAACCCCCCCCCBB
|
||||
color AAAAACCCCCCCCBBB
|
||||
color AAAAACCCCCCCBBBB
|
||||
color AAABBCCCCCCBBBBB
|
||||
color AABBBCCCCCCBBBBB
|
||||
color ABBBBCCCCCBBBBBB
|
||||
color BBBBBCCCCBBBBBBB
|
||||
color BBBBBBCCBBBBBBBB
|
||||
color BBBBBBCBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case src_SourceColor
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 14 127 226 36 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCCCCCCC
|
||||
color AAAACCCCCCCCCCCB
|
||||
color AAAAACCCCCCCCCBB
|
||||
color AAAAACCCCCCCCBBB
|
||||
color AAAAACCCCCCCBBBB
|
||||
color AAABBCCCCCCBBBBB
|
||||
color AABBBCCCCCCBBBBB
|
||||
color ABBBBCCCCCBBBBBB
|
||||
color BBBBBCCCCBBBBBBB
|
||||
color BBBBBBCCBBBBBBBB
|
||||
color BBBBBBCBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case src_OneMinusSourceColor
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 46 53 14 60 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCCCCCCC
|
||||
color AAAACCCCCCCCCCCB
|
||||
color AAAAACCCCCCCCCBB
|
||||
color AAAAACCCCCCCCBBB
|
||||
color AAAAACCCCCCCBBBB
|
||||
color AAABBCCCCCCBBBBB
|
||||
color AABBBCCCCCCBBBBB
|
||||
color ABBBBCCCCCBBBBBB
|
||||
color BBBBBCCCCBBBBBBB
|
||||
color BBBBBBCCBBBBBBBB
|
||||
color BBBBBBCBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case src_SourceAlpha
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 23 68 90 36 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCCCCCCC
|
||||
color AAAACCCCCCCCCCCB
|
||||
color AAAAACCCCCCCCCBB
|
||||
color AAAAACCCCCCCCBBB
|
||||
color AAAAACCCCCCCBBBB
|
||||
color AAABBCCCCCCBBBBB
|
||||
color AABBBCCCCCCBBBBB
|
||||
color ABBBBCCCCCBBBBBB
|
||||
color BBBBBCCCCBBBBBBB
|
||||
color BBBBBBCCBBBBBBBB
|
||||
color BBBBBBCBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case src_OneMinusSourceAlpha
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 37 112 150 60 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCCCCCCC
|
||||
color AAAACCCCCCCCCCCB
|
||||
color AAAAACCCCCCCCCBB
|
||||
color AAAAACCCCCCCCBBB
|
||||
color AAAAACCCCCCCBBBB
|
||||
color AAABBCCCCCCBBBBB
|
||||
color AABBBCCCCCCBBBBB
|
||||
color ABBBBCCCCCBBBBBB
|
||||
color BBBBBCCCCBBBBBBB
|
||||
color BBBBBBCCBBBBBBBB
|
||||
color BBBBBBCBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case src_DestinationColor
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 47 71 47 60 rrrr
|
||||
palette D 8 45 90 48 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case src_OneMinusDestinationColor
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 13 109 193 36 rrrr
|
||||
palette D 52 135 150 48 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case src_DestinationAlpha
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 38 113 151 60 rrrr
|
||||
palette D 30 90 120 48 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case src_OneMinusDestinationAlpha
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 22 67 89 36 rrrr
|
||||
palette D 30 90 120 48 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case dst_Zero
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 0 0 0 0 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCCCCCCC
|
||||
color AAAACCCCCCCCCCCB
|
||||
color AAAAACCCCCCCCCBB
|
||||
color AAAAACCCCCCCCBBB
|
||||
color AAAAACCCCCCCBBBB
|
||||
color AAABBCCCCCCBBBBB
|
||||
color AABBBCCCCCCBBBBB
|
||||
color ABBBBCCCCCBBBBBB
|
||||
color BBBBBCCCCBBBBBBB
|
||||
color BBBBBBCCBBBBBBBB
|
||||
color BBBBBBCBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case dst_One
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAAAAAAAAABBBBB
|
||||
color AAAAAAAAAABBBBBB
|
||||
color AAAAAAAAABBBBBBB
|
||||
color AAAAAAABBBBBBBBB
|
||||
color AAAAAABBBBBBBBBB
|
||||
color AAAAABBBBBBBBBBB
|
||||
color AAABBBBBBBBBBBBB
|
||||
color AABBBBBBBBBBBBBB
|
||||
color ABBBBBBBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case dst_SourceColor
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 47 71 47 60 rrrr
|
||||
palette D 8 45 90 48 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case dst_OneMinusSourceColor
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 153 29 3 100 rrrr
|
||||
palette D 24 19 6 80 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case dst_SourceAlpha
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 75 38 19 60 rrrr
|
||||
palette D 12 24 36 48 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case dst_OneMinusSourceAlpha
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 125 62 31 100 rrrr
|
||||
palette D 20 40 60 80 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case dst_DestinationColor
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 157 39 10 100 rrrr
|
||||
palette D 4 16 36 64 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case dst_OneMinusDestinationColor
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 43 61 40 60 rrrr
|
||||
palette D 28 48 60 64 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case dst_DestinationAlpha
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 125 63 31 100 rrrr
|
||||
palette D 16 32 48 64 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case dst_OneMinusDestinationAlpha
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 75 37 19 60 rrrr
|
||||
palette D 16 32 48 64 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case op_Add
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 147 130 122 136 rrrr
|
||||
palette D 43 108 150 116 rrrr
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case op_Subtract
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 0 5 59 0 erre
|
||||
palette D 3 28 30 0 rrre
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case op_ReverseSubtract
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 102 0 0 64 reer
|
||||
palette D 0 0 0 44 eeer
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case op_Min
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 60 100 50 96 eeee
|
||||
palette D 32 64 96 96 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case op_Max
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 200 180 240 160 eeee
|
||||
palette D 60 180 240 128 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case additive_clamp
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 255 255 255 255 eeee
|
||||
palette D 92 244 255 224 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case separate_alpha
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 147 130 122 96 rrre
|
||||
palette D 43 108 150 96 rrre
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case mask_R
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 60 100 50 160 eeee
|
||||
palette D 60 64 96 128 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case mask_G
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 200 180 50 160 eeee
|
||||
palette D 32 180 96 128 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case mask_B
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 200 100 240 160 eeee
|
||||
palette D 32 64 240 128 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case mask_A
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 200 100 50 96 eeee
|
||||
palette D 32 64 96 96 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case mask_RGB
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 60 180 240 160 eeee
|
||||
palette D 60 180 240 128 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case mask_none
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAAAAAAAAABBBBB
|
||||
color AAAAAAAAAABBBBBB
|
||||
color AAAAAAAAABBBBBBB
|
||||
color AAAAAAABBBBBBBBB
|
||||
color AAAAAABBBBBBBBBB
|
||||
color AAAAABBBBBBBBBBB
|
||||
color AAABBBBBBBBBBBBB
|
||||
color AABBBBBBBBBBBBBB
|
||||
color ABBBBBBBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case mask_RGBA
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 60 180 240 96 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCCCCCCC
|
||||
color AAAACCCCCCCCCCCB
|
||||
color AAAAACCCCCCCCCBB
|
||||
color AAAAACCCCCCCCBBB
|
||||
color AAAAACCCCCCCBBBB
|
||||
color AAABBCCCCCCBBBBB
|
||||
color AABBBCCCCCCBBBBB
|
||||
color ABBBBCCCCCBBBBBB
|
||||
color BBBBBCCCCBBBBBBB
|
||||
color BBBBBBCCBBBBBBBB
|
||||
color BBBBBBCBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case mask_RG_blended
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 147 130 50 160 rree
|
||||
palette D 43 108 96 128 rree
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case separate_operation
|
||||
palette A 200 100 50 160 eeee
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 147 130 122 64 rrre
|
||||
palette D 43 108 150 32 rrre
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
case first_draw_blended
|
||||
palette A 32 87 67 148 errr
|
||||
palette B 32 64 96 128 eeee
|
||||
palette C 60 180 67 148 eerr
|
||||
palette D 60 180 96 128 eeee
|
||||
color AAAAAAAAAAAAAAAB
|
||||
color AAAAAAAAAAAAAABB
|
||||
color AAAAAAAAAAAAABBB
|
||||
color AAAACCCCCCABBBBB
|
||||
color AAAACCCCCCDDDDDD
|
||||
color AAAACCCCCDDDDDDB
|
||||
color AAAAACCDDDDDDDBB
|
||||
color AAAAACDDDDDDDBBB
|
||||
color AAAAADDDDDDDBBBB
|
||||
color AAABBDDDDDDBBBBB
|
||||
color AABBBDDDDDDBBBBB
|
||||
color ABBBBDDDDDBBBBBB
|
||||
color BBBBBDDDDBBBBBBB
|
||||
color BBBBBBDDBBBBBBBB
|
||||
color BBBBBBDBBBBBBBBB
|
||||
color BBBBBBBBBBBBBBBB
|
||||
end
|
||||
@@ -0,0 +1,150 @@
|
||||
use xenora_metal::{BlendDraw, BlendFactor, BlendOperation, BlendPass, BlendState, ColorWriteMask};
|
||||
use xenora_ref::blend::{RefBlend, RefBlendFactor, RefBlendOperation};
|
||||
|
||||
use super::blend_cases::{CLEAR, Case, SIZE, cases, expected};
|
||||
use super::{context, native};
|
||||
|
||||
/// Tolerance, fixed before any native measurement: a channel whose exact
|
||||
/// result is a whole unorm8 value must match exactly; a channel that needs
|
||||
/// rounding may differ by at most this much, because Metal does not document
|
||||
/// how it rounds blended unorm values.
|
||||
const ROUNDING_TOLERANCE: u8 = 1;
|
||||
|
||||
fn factor(factor: RefBlendFactor) -> BlendFactor {
|
||||
match factor {
|
||||
RefBlendFactor::Zero => BlendFactor::Zero,
|
||||
RefBlendFactor::One => BlendFactor::One,
|
||||
RefBlendFactor::SourceColor => BlendFactor::SourceColor,
|
||||
RefBlendFactor::OneMinusSourceColor => BlendFactor::OneMinusSourceColor,
|
||||
RefBlendFactor::SourceAlpha => BlendFactor::SourceAlpha,
|
||||
RefBlendFactor::OneMinusSourceAlpha => BlendFactor::OneMinusSourceAlpha,
|
||||
RefBlendFactor::DestinationColor => BlendFactor::DestinationColor,
|
||||
RefBlendFactor::OneMinusDestinationColor => BlendFactor::OneMinusDestinationColor,
|
||||
RefBlendFactor::DestinationAlpha => BlendFactor::DestinationAlpha,
|
||||
RefBlendFactor::OneMinusDestinationAlpha => BlendFactor::OneMinusDestinationAlpha,
|
||||
}
|
||||
}
|
||||
|
||||
fn operation(operation: RefBlendOperation) -> BlendOperation {
|
||||
match operation {
|
||||
RefBlendOperation::Add => BlendOperation::Add,
|
||||
RefBlendOperation::Subtract => BlendOperation::Subtract,
|
||||
RefBlendOperation::ReverseSubtract => BlendOperation::ReverseSubtract,
|
||||
RefBlendOperation::Min => BlendOperation::Min,
|
||||
RefBlendOperation::Max => BlendOperation::Max,
|
||||
}
|
||||
}
|
||||
|
||||
fn state(blend: RefBlend) -> BlendState {
|
||||
BlendState {
|
||||
source_rgb: factor(blend.source_rgb),
|
||||
destination_rgb: factor(blend.destination_rgb),
|
||||
rgb_operation: operation(blend.rgb_operation),
|
||||
source_alpha: factor(blend.source_alpha),
|
||||
destination_alpha: factor(blend.destination_alpha),
|
||||
alpha_operation: operation(blend.alpha_operation),
|
||||
}
|
||||
}
|
||||
|
||||
fn metal_draws(case: &Case) -> Vec<BlendDraw> {
|
||||
case.draws
|
||||
.iter()
|
||||
.map(|draw| BlendDraw {
|
||||
positions: draw.positions,
|
||||
color: draw.color,
|
||||
blend: draw.blend.map(state),
|
||||
write_mask: ColorWriteMask {
|
||||
red: draw.mask[0],
|
||||
green: draw.mask[1],
|
||||
blue: draw.mask[2],
|
||||
alpha: draw.mask[3],
|
||||
},
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Each case's native colour must equal the independently derived image,
|
||||
/// per channel: exactly where the exact result is a whole unorm8 value, and
|
||||
/// within [`ROUNDING_TOLERANCE`] where it needs rounding.
|
||||
#[test]
|
||||
fn blend_known_answers() {
|
||||
let gpu = context();
|
||||
let (cases, expected) = (cases(), expected());
|
||||
assert_eq!(cases.len(), expected.len());
|
||||
let (mut rounded, mut off_by_one, mut largest) = (0usize, 0usize, 0u8);
|
||||
for (case, want) in cases.iter().zip(&expected) {
|
||||
assert_eq!(case.name, want.name);
|
||||
let draws = metal_draws(case);
|
||||
let color = native(gpu.render_blend(&BlendPass {
|
||||
extent: [SIZE, SIZE],
|
||||
clear_color: CLEAR,
|
||||
draws: &draws,
|
||||
}));
|
||||
assert_eq!(color.len(), SIZE * SIZE * 4);
|
||||
for (index, ((pixel, wanted), exact)) in color
|
||||
.as_chunks::<4>()
|
||||
.0
|
||||
.iter()
|
||||
.zip(&want.color)
|
||||
.zip(&want.exact)
|
||||
.enumerate()
|
||||
{
|
||||
let (x, y) = (index % SIZE, index / SIZE);
|
||||
for channel in 0..4 {
|
||||
let difference = pixel[channel].abs_diff(wanted[channel]);
|
||||
let allowed = if exact[channel] {
|
||||
0
|
||||
} else {
|
||||
ROUNDING_TOLERANCE
|
||||
};
|
||||
assert!(
|
||||
difference <= allowed,
|
||||
"{} at ({x}, {y}) channel {channel}: {pixel:?} vs {wanted:?} (allowed {allowed})",
|
||||
case.name
|
||||
);
|
||||
if !exact[channel] {
|
||||
rounded += 1;
|
||||
off_by_one += usize::from(difference == 1);
|
||||
largest = largest.max(difference);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!(
|
||||
"checked {} blend passes; {rounded} rounded channels, {off_by_one} off by one, largest difference {largest}",
|
||||
cases.len()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_many_blend_draws_are_rejected() {
|
||||
let gpu = context();
|
||||
let draw = BlendDraw {
|
||||
positions: [
|
||||
[-1.0, -1.0, 0.5, 1.0],
|
||||
[3.0, -1.0, 0.5, 1.0],
|
||||
[-1.0, 3.0, 0.5, 1.0],
|
||||
],
|
||||
color: [255; 4],
|
||||
blend: None,
|
||||
write_mask: ColorWriteMask::ALL,
|
||||
};
|
||||
let error = gpu
|
||||
.render_blend(&BlendPass {
|
||||
extent: [SIZE, SIZE],
|
||||
clear_color: CLEAR,
|
||||
draws: &[draw; 17],
|
||||
})
|
||||
.expect_err("17 draws must be rejected");
|
||||
assert!(error.to_string().contains("at most 16 draws"), "{error}");
|
||||
let mut bad = draw;
|
||||
bad.positions[1][0] = f32::NAN;
|
||||
let error = gpu
|
||||
.render_blend(&BlendPass {
|
||||
extent: [SIZE, SIZE],
|
||||
clear_color: CLEAR,
|
||||
draws: &[bad],
|
||||
})
|
||||
.expect_err("non-finite positions must be rejected");
|
||||
assert!(error.to_string().contains("finite"), "{error}");
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
use xenora_metal::NativeCompute;
|
||||
|
||||
#[path = "native/blend.rs"]
|
||||
mod blend;
|
||||
#[path = "support/blend_cases.rs"]
|
||||
mod blend_cases;
|
||||
#[path = "native/depth.rs"]
|
||||
mod depth;
|
||||
#[path = "support/depth_cases.rs"]
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
//! Original blend-pass fixtures shared by the CPU reference check and the
|
||||
//! native Metal test, and the parser for the independently derived
|
||||
//! expectations in `fixtures/blend_expected.txt`.
|
||||
//!
|
||||
//! The expectations record, per channel, whether the exact result needed
|
||||
//! rounding; the native test's tolerance (`native/blend.rs`) depends on it.
|
||||
|
||||
use xenora_ref::blend::{RefBlend, RefBlendFactor, RefBlendOperation};
|
||||
|
||||
pub const SIZE: usize = 16;
|
||||
pub const CLEAR: [u8; 4] = [32, 64, 96, 128];
|
||||
const DST: [u8; 4] = [200, 100, 50, 160];
|
||||
const SRC: [u8; 4] = [60, 180, 240, 96];
|
||||
|
||||
// Triangles in pixel coordinates; no pixel centre lies on any edge.
|
||||
const A: [[f32; 2]; 3] = [[0.0, 0.0], [16.0, 0.0], [0.0, 12.0]];
|
||||
const B: [[f32; 2]; 3] = [[4.0, 3.0], [16.0, 4.0], [6.0, 16.0]];
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Draw {
|
||||
pub positions: [[f32; 4]; 3],
|
||||
pub color: [u8; 4],
|
||||
pub blend: Option<RefBlend>,
|
||||
/// Written channels R, G, B, A.
|
||||
pub mask: [bool; 4],
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Case {
|
||||
pub name: String,
|
||||
pub draws: Vec<Draw>,
|
||||
}
|
||||
|
||||
fn clip(tri: [[f32; 2]; 3]) -> [[f32; 4]; 3] {
|
||||
tri.map(|[x, y]| [x / 8.0 - 1.0, 1.0 - y / 8.0, 0.5, 1.0])
|
||||
}
|
||||
|
||||
fn same(
|
||||
source: RefBlendFactor,
|
||||
destination: RefBlendFactor,
|
||||
operation: RefBlendOperation,
|
||||
) -> RefBlend {
|
||||
RefBlend {
|
||||
source_rgb: source,
|
||||
destination_rgb: destination,
|
||||
rgb_operation: operation,
|
||||
source_alpha: source,
|
||||
destination_alpha: destination,
|
||||
alpha_operation: operation,
|
||||
}
|
||||
}
|
||||
|
||||
/// Destination triangle A, then source triangle B with `blend` and `mask`.
|
||||
fn case(name: String, blend: Option<RefBlend>, mask: [bool; 4]) -> Case {
|
||||
Case {
|
||||
name,
|
||||
draws: vec![
|
||||
Draw {
|
||||
positions: clip(A),
|
||||
color: DST,
|
||||
blend: None,
|
||||
mask: [true; 4],
|
||||
},
|
||||
Draw {
|
||||
positions: clip(B),
|
||||
color: SRC,
|
||||
blend,
|
||||
mask,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
/// Every case, in fixture order.
|
||||
pub fn cases() -> Vec<Case> {
|
||||
use RefBlendFactor::{One, OneMinusSourceAlpha, SourceAlpha, Zero};
|
||||
use RefBlendOperation::Add;
|
||||
let all = [true; 4];
|
||||
let mut cases = vec![
|
||||
Case {
|
||||
name: "clear".to_owned(),
|
||||
draws: Vec::new(),
|
||||
},
|
||||
case("disabled".to_owned(), None, all),
|
||||
];
|
||||
for factor in RefBlendFactor::ALL {
|
||||
cases.push(case(
|
||||
format!("src_{factor:?}"),
|
||||
Some(same(factor, Zero, Add)),
|
||||
all,
|
||||
));
|
||||
}
|
||||
for factor in RefBlendFactor::ALL {
|
||||
cases.push(case(
|
||||
format!("dst_{factor:?}"),
|
||||
Some(same(Zero, factor, Add)),
|
||||
all,
|
||||
));
|
||||
}
|
||||
for operation in RefBlendOperation::ALL {
|
||||
cases.push(case(
|
||||
format!("op_{operation:?}"),
|
||||
Some(same(SourceAlpha, OneMinusSourceAlpha, operation)),
|
||||
all,
|
||||
));
|
||||
}
|
||||
cases.push(case(
|
||||
"additive_clamp".to_owned(),
|
||||
Some(same(One, One, Add)),
|
||||
all,
|
||||
));
|
||||
cases.push(case(
|
||||
"separate_alpha".to_owned(),
|
||||
Some(RefBlend {
|
||||
source_alpha: One,
|
||||
destination_alpha: Zero,
|
||||
..same(SourceAlpha, OneMinusSourceAlpha, Add)
|
||||
}),
|
||||
all,
|
||||
));
|
||||
for (name, mask) in [
|
||||
("R", [true, false, false, false]),
|
||||
("G", [false, true, false, false]),
|
||||
("B", [false, false, true, false]),
|
||||
("A", [false, false, false, true]),
|
||||
("RGB", [true, true, true, false]),
|
||||
("none", [false; 4]),
|
||||
("RGBA", all),
|
||||
] {
|
||||
cases.push(case(format!("mask_{name}"), None, mask));
|
||||
}
|
||||
cases.push(case(
|
||||
"mask_RG_blended".to_owned(),
|
||||
Some(same(SourceAlpha, OneMinusSourceAlpha, Add)),
|
||||
[true, true, false, false],
|
||||
));
|
||||
let mut first = case(
|
||||
"first_draw_blended".to_owned(),
|
||||
None,
|
||||
[true, true, false, false],
|
||||
);
|
||||
first.draws[0].blend = Some(same(SourceAlpha, OneMinusSourceAlpha, Add));
|
||||
first.draws[0].mask = [false, true, true, true];
|
||||
cases.push(case(
|
||||
"separate_operation".to_owned(),
|
||||
Some(RefBlend {
|
||||
source_alpha: One,
|
||||
destination_alpha: One,
|
||||
alpha_operation: RefBlendOperation::ReverseSubtract,
|
||||
..same(SourceAlpha, OneMinusSourceAlpha, Add)
|
||||
}),
|
||||
all,
|
||||
));
|
||||
cases.push(first);
|
||||
cases
|
||||
}
|
||||
|
||||
/// One case of `blend_expected.txt`: per pixel, the colour and whether each
|
||||
/// channel needed rounding.
|
||||
#[derive(Debug)]
|
||||
pub struct Expected {
|
||||
pub name: String,
|
||||
pub color: Vec<[u8; 4]>,
|
||||
pub exact: Vec<[bool; 4]>,
|
||||
}
|
||||
|
||||
/// Parse the independently derived expectations.
|
||||
pub fn expected() -> Vec<Expected> {
|
||||
let text = include_str!("../fixtures/blend_expected.txt");
|
||||
let mut out = Vec::new();
|
||||
let mut lines = text.lines().filter(|line| !line.trim().is_empty());
|
||||
while let Some(header) = lines.next() {
|
||||
let name = header.strip_prefix("case ").expect("case line").to_owned();
|
||||
let (mut palette, mut color, mut exact) = (Vec::new(), Vec::new(), Vec::new());
|
||||
for line in lines.by_ref() {
|
||||
let words: Vec<&str> = line.split_whitespace().collect();
|
||||
match words[0] {
|
||||
"palette" => {
|
||||
let key = words[1].chars().next().unwrap();
|
||||
let rgba: Vec<u8> = words[2..6].iter().map(|w| w.parse().unwrap()).collect();
|
||||
let flags: Vec<bool> = words[6]
|
||||
.chars()
|
||||
.map(|c| match c {
|
||||
'e' => true,
|
||||
'r' => false,
|
||||
other => panic!("unexpected rounding flag {other}"),
|
||||
})
|
||||
.collect();
|
||||
palette.push((
|
||||
key,
|
||||
<[u8; 4]>::try_from(rgba).unwrap(),
|
||||
<[bool; 4]>::try_from(flags).unwrap(),
|
||||
));
|
||||
}
|
||||
"color" => {
|
||||
for c in words[1].chars() {
|
||||
let entry = palette.iter().find(|(k, ..)| *k == c).expect("palette key");
|
||||
color.push(entry.1);
|
||||
exact.push(entry.2);
|
||||
}
|
||||
}
|
||||
"end" => break,
|
||||
other => panic!("unexpected fixture line {other}"),
|
||||
}
|
||||
}
|
||||
assert_eq!(color.len(), SIZE * SIZE, "{name} colour rows");
|
||||
out.push(Expected { name, color, exact });
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
//! Independent CPU reference for blended, write-masked triangle draws into an
|
||||
//! RGBA8 unorm target.
|
||||
//!
|
||||
//! Every value is kept exact: a unorm8 value `v` means `v / 255`, so a
|
||||
//! weighted term `value * factor` is an integer count of `1 / 255²`. The
|
||||
//! result is clamped to 0..=1 and rounded to the nearest unorm8 value. The
|
||||
//! reference also reports, per channel, whether that rounding was needed, so
|
||||
//! a native comparison can hold exact results to exact equality. Coverage
|
||||
//! comes from [`crate::depth::render_depth`].
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::depth::{CompareFunction, DepthRefError, RefDraw, RefFill, render_depth};
|
||||
|
||||
/// A blend factor; the colour factors use the alpha channel's own value for alpha.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum RefBlendFactor {
|
||||
Zero,
|
||||
One,
|
||||
SourceColor,
|
||||
OneMinusSourceColor,
|
||||
SourceAlpha,
|
||||
OneMinusSourceAlpha,
|
||||
DestinationColor,
|
||||
OneMinusDestinationColor,
|
||||
DestinationAlpha,
|
||||
OneMinusDestinationAlpha,
|
||||
}
|
||||
|
||||
impl RefBlendFactor {
|
||||
pub const ALL: [Self; 10] = [
|
||||
Self::Zero,
|
||||
Self::One,
|
||||
Self::SourceColor,
|
||||
Self::OneMinusSourceColor,
|
||||
Self::SourceAlpha,
|
||||
Self::OneMinusSourceAlpha,
|
||||
Self::DestinationColor,
|
||||
Self::OneMinusDestinationColor,
|
||||
Self::DestinationAlpha,
|
||||
Self::OneMinusDestinationAlpha,
|
||||
];
|
||||
|
||||
/// The factor for `channel` in units of 1/255.
|
||||
fn value(self, source: [u8; 4], destination: [u8; 4], channel: usize) -> i64 {
|
||||
let (s, d) = (i64::from(source[channel]), i64::from(destination[channel]));
|
||||
let (sa, da) = (i64::from(source[3]), i64::from(destination[3]));
|
||||
match self {
|
||||
Self::Zero => 0,
|
||||
Self::One => 255,
|
||||
Self::SourceColor => s,
|
||||
Self::OneMinusSourceColor => 255 - s,
|
||||
Self::SourceAlpha => sa,
|
||||
Self::OneMinusSourceAlpha => 255 - sa,
|
||||
Self::DestinationColor => d,
|
||||
Self::OneMinusDestinationColor => 255 - d,
|
||||
Self::DestinationAlpha => da,
|
||||
Self::OneMinusDestinationAlpha => 255 - da,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A blend operation; `Min` and `Max` ignore the factors.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum RefBlendOperation {
|
||||
Add,
|
||||
Subtract,
|
||||
ReverseSubtract,
|
||||
Min,
|
||||
Max,
|
||||
}
|
||||
|
||||
impl RefBlendOperation {
|
||||
pub const ALL: [Self; 5] = [
|
||||
Self::Add,
|
||||
Self::Subtract,
|
||||
Self::ReverseSubtract,
|
||||
Self::Min,
|
||||
Self::Max,
|
||||
];
|
||||
}
|
||||
|
||||
/// Separate RGB and alpha blend equations.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct RefBlend {
|
||||
pub source_rgb: RefBlendFactor,
|
||||
pub destination_rgb: RefBlendFactor,
|
||||
pub rgb_operation: RefBlendOperation,
|
||||
pub source_alpha: RefBlendFactor,
|
||||
pub destination_alpha: RefBlendFactor,
|
||||
pub alpha_operation: RefBlendOperation,
|
||||
}
|
||||
|
||||
/// One flat-coloured triangle; `mask` lists the written channels R, G, B, A.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct RefBlendDraw {
|
||||
pub positions: [[f32; 4]; 3],
|
||||
pub color: [u8; 4],
|
||||
pub blend: Option<RefBlend>,
|
||||
pub mask: [bool; 4],
|
||||
}
|
||||
|
||||
/// A blended channel value and whether rounding was needed to reach it.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct RefChannel {
|
||||
pub value: u8,
|
||||
pub exact: bool,
|
||||
}
|
||||
|
||||
/// Blend one channel (0 to 3: R, G, B, A) of `source` over `destination`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics when `channel` is above 3.
|
||||
pub fn blend_channel(
|
||||
blend: RefBlend,
|
||||
source: [u8; 4],
|
||||
destination: [u8; 4],
|
||||
channel: usize,
|
||||
) -> RefChannel {
|
||||
let (factors, operation) = if channel == 3 {
|
||||
(
|
||||
(blend.source_alpha, blend.destination_alpha),
|
||||
blend.alpha_operation,
|
||||
)
|
||||
} else {
|
||||
(
|
||||
(blend.source_rgb, blend.destination_rgb),
|
||||
blend.rgb_operation,
|
||||
)
|
||||
};
|
||||
let (s, d) = (i64::from(source[channel]), i64::from(destination[channel]));
|
||||
let weighted_s = s * factors.0.value(source, destination, channel);
|
||||
let weighted_d = d * factors.1.value(source, destination, channel);
|
||||
// In units of 1/255².
|
||||
let result = match operation {
|
||||
RefBlendOperation::Add => weighted_s + weighted_d,
|
||||
RefBlendOperation::Subtract => weighted_s - weighted_d,
|
||||
RefBlendOperation::ReverseSubtract => weighted_d - weighted_s,
|
||||
RefBlendOperation::Min => s.min(d) * 255,
|
||||
RefBlendOperation::Max => s.max(d) * 255,
|
||||
}
|
||||
.clamp(0, 255 * 255);
|
||||
RefChannel {
|
||||
// Round half up: floor(result / 255 + 1/2).
|
||||
value: u8::try_from((2 * result + 255) / 510).expect("clamped to 0..=255"),
|
||||
exact: result % 255 == 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum BlendRefError {
|
||||
/// The extent is empty or holds more than 4096 pixels, the native limit.
|
||||
Extent,
|
||||
/// The triangle coverage could not be computed.
|
||||
Raster(DepthRefError),
|
||||
}
|
||||
|
||||
impl fmt::Display for BlendRefError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{self:?}")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for BlendRefError {}
|
||||
|
||||
/// Render `draws` in order over a target cleared to `clear_color`, returning
|
||||
/// each pixel's channels and whether each needed rounding (a cleared or
|
||||
/// replaced channel is exact).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error for an extent that is empty or above 4096 pixels, or when a triangle's
|
||||
/// coverage cannot be computed (see [`render_depth`]).
|
||||
pub fn render_blend(
|
||||
extent: [usize; 2],
|
||||
clear_color: [u8; 4],
|
||||
draws: &[RefBlendDraw],
|
||||
) -> Result<Vec<[RefChannel; 4]>, BlendRefError> {
|
||||
let exact = |value| RefChannel { value, exact: true };
|
||||
let pixels = extent[0]
|
||||
.checked_mul(extent[1])
|
||||
.filter(|&n| (1..=4096).contains(&n))
|
||||
.ok_or(BlendRefError::Extent)?;
|
||||
let mut target = vec![clear_color.map(exact); pixels];
|
||||
for draw in draws {
|
||||
// Coverage: a white triangle over black, without depth attachment.
|
||||
let (coverage, _) = render_depth(
|
||||
extent,
|
||||
[0; 4],
|
||||
None,
|
||||
&[RefDraw {
|
||||
positions: draw.positions,
|
||||
fill: RefFill::Flat([255; 4]),
|
||||
compare: CompareFunction::Always,
|
||||
write: false,
|
||||
}],
|
||||
None,
|
||||
)
|
||||
.map_err(BlendRefError::Raster)?;
|
||||
for (pixel, covered) in target.iter_mut().zip(coverage) {
|
||||
if covered != [255; 4] {
|
||||
continue;
|
||||
}
|
||||
let destination = pixel.map(|channel| channel.value);
|
||||
for (channel, slot) in pixel.iter_mut().enumerate() {
|
||||
if !draw.mask[channel] {
|
||||
continue;
|
||||
}
|
||||
*slot = match draw.blend {
|
||||
None => exact(draw.color[channel]),
|
||||
Some(blend) => blend_channel(blend, draw.color, destination, channel),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(target)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use RefBlendFactor::{One, OneMinusSourceAlpha, SourceAlpha, Zero};
|
||||
use RefBlendOperation::{Add, Max, Min, ReverseSubtract, Subtract};
|
||||
|
||||
fn blend(
|
||||
source: RefBlendFactor,
|
||||
destination: RefBlendFactor,
|
||||
operation: RefBlendOperation,
|
||||
) -> RefBlend {
|
||||
RefBlend {
|
||||
source_rgb: source,
|
||||
destination_rgb: destination,
|
||||
rgb_operation: operation,
|
||||
source_alpha: source,
|
||||
destination_alpha: destination,
|
||||
alpha_operation: operation,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_extents_are_rejected() {
|
||||
for extent in [[0, 4], [4, 0], [usize::MAX, 2], [usize::MAX, 1], [65, 64]] {
|
||||
assert_eq!(
|
||||
render_blend(extent, [0; 4], &[]),
|
||||
Err(BlendRefError::Extent)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn channels_follow_the_blend_equation() {
|
||||
let (s, d) = ([60, 180, 240, 96], [200, 100, 50, 160]);
|
||||
let channel = |state, c| blend_channel(state, s, d, c);
|
||||
// 60 * 96 / 255 + 200 * 159 / 255 = 147.29...
|
||||
assert_eq!(
|
||||
channel(blend(SourceAlpha, OneMinusSourceAlpha, Add), 0),
|
||||
RefChannel {
|
||||
value: 147,
|
||||
exact: false
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
channel(blend(One, Zero, Add), 1),
|
||||
RefChannel {
|
||||
value: 180,
|
||||
exact: true
|
||||
}
|
||||
);
|
||||
// 240 + 50 clamps to 255; 60 - 200 clamps to 0.
|
||||
assert_eq!(
|
||||
channel(blend(One, One, Add), 2),
|
||||
RefChannel {
|
||||
value: 255,
|
||||
exact: true
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
channel(blend(One, One, Subtract), 0),
|
||||
RefChannel {
|
||||
value: 0,
|
||||
exact: true
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
channel(blend(One, One, ReverseSubtract), 0),
|
||||
RefChannel {
|
||||
value: 140,
|
||||
exact: true
|
||||
}
|
||||
);
|
||||
// Min and Max ignore the factors.
|
||||
assert_eq!(
|
||||
channel(blend(Zero, Zero, Min), 1),
|
||||
RefChannel {
|
||||
value: 100,
|
||||
exact: true
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
channel(blend(Zero, Zero, Max), 3),
|
||||
RefChannel {
|
||||
value: 160,
|
||||
exact: true
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
//! Independent CPU execution of the synthetic gfx1030 fixture subset, reference sampling, depth-tested rendering and indexed draws.
|
||||
//! Independent CPU execution of the synthetic gfx1030 fixture subset, reference sampling, and host-side references for depth-tested rendering, indexed draws and blending.
|
||||
|
||||
pub mod blend;
|
||||
pub mod depth;
|
||||
pub mod indexed;
|
||||
pub mod texture_sampling;
|
||||
|
||||
@@ -17,6 +17,12 @@ xenora-shader = { path = "../crates/shader" }
|
||||
|
||||
[workspace]
|
||||
|
||||
[[bin]]
|
||||
name = "cpu_decode"
|
||||
path = "fuzz_targets/cpu_decode.rs"
|
||||
test = false
|
||||
doc = false
|
||||
|
||||
[[bin]]
|
||||
name = "instruction_stream"
|
||||
path = "fuzz_targets/instruction_stream.rs"
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#![no_main]
|
||||
|
||||
use libfuzzer_sys::fuzz_target;
|
||||
use std::collections::BTreeSet;
|
||||
use xenora_loader::probe::{ProbeConfig, probe_pe_bytes};
|
||||
|
||||
const MAX_CODE_BYTES: usize = 4096;
|
||||
const CONFIG: ProbeConfig = ProbeConfig {
|
||||
max_disasm_bytes: MAX_CODE_BYTES,
|
||||
max_instructions: 2048,
|
||||
max_worklist_entries: 1024,
|
||||
};
|
||||
|
||||
const PE_OFFSET: usize = 0x40;
|
||||
const COFF: usize = PE_OFFSET + 4;
|
||||
const OPTIONAL: usize = COFF + 20;
|
||||
const OPTIONAL_LEN: u16 = 0xf0;
|
||||
const TEXT: usize = OPTIONAL + OPTIONAL_LEN as usize;
|
||||
const HEADER_LEN: usize = 0x200;
|
||||
const TEXT_RVA: u32 = 0x1000;
|
||||
const SECTION_ALIGNMENT: u32 = 0x1000;
|
||||
|
||||
const HEADER: [u8; HEADER_LEN] = {
|
||||
let mut header = [0; HEADER_LEN];
|
||||
write_field(&mut header, 0, b"MZ");
|
||||
write_field(&mut header, 0x3c, &(PE_OFFSET as u32).to_le_bytes());
|
||||
write_field(&mut header, PE_OFFSET, b"PE\0\0");
|
||||
write_field(&mut header, COFF, &0x8664u16.to_le_bytes()); // Machine
|
||||
write_field(&mut header, COFF + 2, &1u16.to_le_bytes()); // NumberOfSections
|
||||
write_field(&mut header, COFF + 16, &OPTIONAL_LEN.to_le_bytes());
|
||||
write_field(&mut header, OPTIONAL, &0x20bu16.to_le_bytes()); // Magic
|
||||
write_field(&mut header, OPTIONAL + 16, &TEXT_RVA.to_le_bytes()); // AddressOfEntryPoint
|
||||
write_field(&mut header, OPTIONAL + 20, &TEXT_RVA.to_le_bytes()); // BaseOfCode
|
||||
write_field(&mut header, OPTIONAL + 24, &0x1_4000_0000u64.to_le_bytes()); // ImageBase
|
||||
write_field(&mut header, OPTIONAL + 32, &SECTION_ALIGNMENT.to_le_bytes());
|
||||
write_field(&mut header, OPTIONAL + 36, &0x200u32.to_le_bytes()); // FileAlignment
|
||||
write_field(
|
||||
&mut header,
|
||||
OPTIONAL + 60,
|
||||
&(HEADER_LEN as u32).to_le_bytes(),
|
||||
); // SizeOfHeaders
|
||||
write_field(&mut header, OPTIONAL + 108, &16u32.to_le_bytes()); // NumberOfRvaAndSizes
|
||||
write_field(&mut header, TEXT, b".text");
|
||||
write_field(&mut header, TEXT + 12, &TEXT_RVA.to_le_bytes()); // VirtualAddress
|
||||
write_field(&mut header, TEXT + 20, &(HEADER_LEN as u32).to_le_bytes()); // PointerToRawData
|
||||
write_field(&mut header, TEXT + 36, &0x6000_0020u32.to_le_bytes()); // CODE | EXECUTE | READ
|
||||
header
|
||||
};
|
||||
|
||||
const fn write_field(header: &mut [u8], offset: usize, bytes: &[u8]) {
|
||||
let (_, tail) = header.split_at_mut(offset);
|
||||
let (field, _) = tail.split_at_mut(bytes.len());
|
||||
field.copy_from_slice(bytes);
|
||||
}
|
||||
|
||||
fn build_image(payload: &[u8]) -> Vec<u8> {
|
||||
let mut header = HEADER;
|
||||
let code_size = payload.len() as u32;
|
||||
let image_size = TEXT_RVA + code_size.next_multiple_of(SECTION_ALIGNMENT);
|
||||
write_field(&mut header, OPTIONAL + 4, &code_size.to_le_bytes()); // SizeOfCode
|
||||
write_field(&mut header, OPTIONAL + 56, &image_size.to_le_bytes()); // SizeOfImage
|
||||
write_field(&mut header, TEXT + 8, &code_size.to_le_bytes()); // VirtualSize
|
||||
write_field(&mut header, TEXT + 16, &code_size.to_le_bytes()); // SizeOfRawData
|
||||
|
||||
// The probe decodes up to EOF, so leave the payload unpadded.
|
||||
let mut image = Vec::with_capacity(HEADER_LEN + payload.len());
|
||||
image.extend_from_slice(&header);
|
||||
image.extend_from_slice(payload);
|
||||
image
|
||||
}
|
||||
|
||||
fuzz_target!(init: {
|
||||
let image = build_image(&[0x90, 0xc3]);
|
||||
let report = probe_pe_bytes(&image, &CONFIG).expect("startup PE probe succeeds");
|
||||
let records = report.instruction_records;
|
||||
assert!(records.len() == 2, "startup decodes two instructions");
|
||||
assert!(records[0].rva == TEXT_RVA, "startup decodes entry RVA");
|
||||
assert!(records[1].rva == TEXT_RVA + 1, "startup decodes next RVA");
|
||||
}, |data: &[u8]| {
|
||||
let payload = &data[..data.len().min(MAX_CODE_BYTES)];
|
||||
let image = build_image(payload);
|
||||
let a = probe_pe_bytes(&image, &CONFIG);
|
||||
let b = probe_pe_bytes(&image, &CONFIG);
|
||||
assert!(format!("{a:?}") == format!("{b:?}"), "probe is deterministic");
|
||||
|
||||
if let Ok(report) = a {
|
||||
let records = report.instruction_records;
|
||||
assert!(records.len() <= CONFIG.max_instructions, "instruction budget");
|
||||
let code_range = TEXT_RVA..TEXT_RVA + payload.len() as u32;
|
||||
let mut rvas = BTreeSet::new();
|
||||
for record in records {
|
||||
let len = record.bytes.len();
|
||||
assert!((1..=15).contains(&len), "x86 instruction length");
|
||||
assert!(code_range.contains(&record.rva), "RVA within payload");
|
||||
assert!(rvas.insert(record.rva), "RVA is unique");
|
||||
let start = (record.rva - TEXT_RVA) as usize;
|
||||
let end = start + len;
|
||||
assert!(end <= payload.len(), "instruction ends within payload");
|
||||
assert!(record.bytes == payload[start..end], "instruction bytes match payload");
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user