Check the GPU's effects analytically instead of against the CPU baking

This commit is contained in:
Keavon Chambers committed 2026-12-05 13:30:51 -08:00
1 parent 98d636c1e5
commit 5418fcbf7b
15 files changed
+2582 -1102

No files matched your search

+2
View File
@@ -176,6 +176,8 @@ jobs:
env:
# `--cfg=web_sys_unstable_apis` mirrors the `[build]` section of `.cargo/config.toml`
RUSTFLAGS: "-Dwarnings --cfg=web_sys_unstable_apis"
# GPU tests made through `for_tests()` fail rather than skip when the software Vulkan driver above can't make a device
WGPU_BACKEND: vulkan
# The desktop crates need CEF, and `third-party-licenses` only pulls it in through its `desktop` feature, so it runs on its own without `--all-features`
run: >-
cargo test --workspace --all-features
Generated
+2
View File
@@ -585,6 +585,7 @@ name = "blending-nodes"
version = "0.0.0"
dependencies = [
"core-types",
"futures",
"glam",
"graphic-types",
"node-macro",
@@ -592,6 +593,7 @@ dependencies = [
"serde",
"text-nodes",
"vector-types",
"wgpu-executor",
]
[[package]]
@@ -480,10 +480,8 @@ mod tests {
#[test]
fn values_a_blend_if_hides_stay_hidden_through_adjustments_raising_alpha_as_rasters_do() {
use crate::raster_effects::{evaluate_raster_effects, premultiplied_gamma_channels};
use graphic_types::EffectKind;
use graphic_types::blend_if::BlendIfRanges;
use raster_types::{Image, Raster};
// Shown only where the gray is at least half
let mut hides_darks = BlendIfRanges::default();
@@ -497,15 +495,8 @@ mod tests {
opacity: 100.,
});
// Black consumed as a color, which agrees with a raster's pixel of it
let black_through = |name: &str, rows: &[(EffectOperation, f64)]| {
let color = *evaluate_color_effects(stamped(Color::BLACK, rows)).element();
let raster = evaluate_raster_effects(stamped(Raster::new_cpu(Image::new(1, 1, Color::BLACK)), rows));
let pixel = raster.element().data.first().copied().unwrap_or(Color::TRANSPARENT);
let (color_channels, pixel_channels) = (premultiplied_gamma_channels(color), premultiplied_gamma_channels(pixel));
assert!(close(color_channels, pixel_channels), "black {name}: {color_channels:?} vs the raster's {pixel_channels:?}");
color
};
// Black consumed as a color, which the GPU's render of a raster of it agrees with (`blend_ifs_hide_rasters_through_adjustments_raising_alpha_as_they_hide_values` in wgpu-executor)
let black_through = |rows: &[(EffectOperation, f64)]| *evaluate_color_effects(stamped(Color::BLACK, rows)).element();
// Hidden black stays hidden when made opaque, in its run or the next, where an overlay finds nothing to paint within either
let hidden = [
@@ -514,11 +505,11 @@ mod tests {
("made opaque and painted in the next run", vec![(blend_if.clone(), 1.), (make_opaque.clone(), 2.), (red.clone(), 2.)]),
];
for (name, rows) in hidden {
assert_eq!(black_through(name, &rows).a(), 0., "black {name}");
assert_eq!(black_through(&rows).a(), 0., "black {name}");
}
// In its own run, an overlay still paints within the full-strength black
let painted = black_through("made opaque and painted", &[(blend_if.clone(), 1.), (make_opaque.clone(), 1.), (red, 1.)]);
let painted = black_through(&[(blend_if.clone(), 1.), (make_opaque.clone(), 1.), (red, 1.)]);
assert!(close(gamma(painted), [1., 0., 0., 1.]), "{:?}", gamma(painted));
// A ramp made opaque stays hidden where it's dark, in the Blend If's run or the next
@@ -21,8 +21,7 @@ impl Context {
}
/// The context the GPU tests run on, which wgpu's environment variables can aim at each backend and shader compiler in turn.
#[cfg(test)]
pub(crate) async fn for_tests() -> Option<Self> {
pub async fn for_tests() -> Option<Self> {
let context = ContextBuilder::new().with_environment_overrides().build().await;
// A backend asked for by name fails the tests if it can't run them, rather than passing them all as skipped
@@ -199,6 +199,12 @@ impl WgpuExecutor {
Self::with_context(WgpuContext::new().await?)
}
/// The executor GPU tests run on, on the context [`WgpuContext::for_tests`] makes, or `None` without a GPU.
#[cfg(not(target_family = "wasm"))]
pub async fn for_tests() -> Option<Self> {
Self::with_context(WgpuContext::for_tests().await?)
}
pub fn with_context(context: WgpuContext) -> Option<Self> {
let vello_renderer = VelloRenderer::new(&context.device, &context.adapter);
File diff suppressed because it is too large. Load diff
+7
View File
@@ -31,3 +31,10 @@ glam = { workspace = true }
# Optional workspace dependencies
serde = { workspace = true, optional = true }
[dev-dependencies]
# Local dependencies
wgpu-executor = { workspace = true }
# Workspace dependencies
futures = { workspace = true }
+12 -5
View File
@@ -683,7 +683,7 @@ mod test {
use core_types::list::{ATTR_EFFECTS, Item};
use core_types::{ATTR_GRADIENT_END_CURVATURE, ATTR_GRADIENT_START_CURVATURE, ATTR_TRANSFORM, Color};
use glam::{DAffine2, DVec2};
use graphic_types::raster_types::{Image, Raster};
use graphic_types::raster_types::{CPU, Image, Raster};
use graphic_types::{Effects, Graphic};
use vector_types::{Gradient, GradientForm};
@@ -731,6 +731,13 @@ mod test {
);
}
/// The single pixel of the raster `item` with its effects carried out on the GPU and read back, or `None` without a GPU.
fn rendered_pixel(item: Item<Raster<CPU>>) -> Option<Color> {
let executor = futures::executor::block_on(wgpu_executor::WgpuExecutor::for_tests())?;
let rendered = futures::executor::block_on(executor.read_back(item)).expect("the effects render");
Some(rendered.element().data[0])
}
#[test]
fn paint_overlay_multiplies_a_color_into_the_content() {
let image_color = Color::from_rgbaf32_unchecked(0.7, 0.6, 0.5, 0.4);
@@ -745,11 +752,11 @@ mod test {
false.into(),
DAffine2::IDENTITY.into(),
);
let result = raster_kernels::evaluate_raster_effects(result).into_element();
let Some(color) = rendered_pixel(result) else { return };
// Multiplying by green keeps only the green channel. It blends within the content's share, so the translucent content keeps its own alpha.
let expected = Color::from_rgbaf32_unchecked(0., image_color.g(), 0., image_color.a());
assert!((result.data[0].to_vec4() - expected.to_vec4()).abs().max_element() < 1e-4, "{:?} against {expected:?}", result.data[0]);
assert!((color.to_vec4() - expected.to_vec4()).abs().max_element() < 0.01, "{color:?} against {expected:?}");
}
#[test]
@@ -766,7 +773,7 @@ mod test {
false.into(),
DAffine2::IDENTITY.into(),
);
let color = raster_kernels::evaluate_raster_effects(result).into_element().data[0];
let Some(color) = rendered_pixel(result) else { return };
assert!(color.r() > 0.05 && color.r() < 0.95, "half the black paint leaves {color:?}");
}
@@ -1019,7 +1026,7 @@ mod test {
false.into(),
DAffine2::IDENTITY.into(),
);
let color = raster_kernels::evaluate_raster_effects(result).into_element().data[0];
let Some(color) = rendered_pixel(result) else { return };
assert!(color.r() > 0.99, "the hidden black paint leaves {color:?}");
}
+24 -8
View File
@@ -97,9 +97,15 @@ fn feather<T>(
mod test {
use core_types::Color;
use core_types::list::{Item, List};
use graphic_types::raster_types::{Image, Raster};
use graphic_types::raster_types::{CPU, Image, Pixels, Raster};
use graphic_types::{Graphic, MaskMode};
/// The GPU's render of the raster `item` with its effects carried out, read back, or `None` without a GPU.
fn rendered(item: Item<Raster<CPU>>) -> Option<Item<Pixels>> {
let executor = futures::executor::block_on(wgpu_executor::WgpuExecutor::for_tests())?;
Some(futures::executor::block_on(executor.read_back(item)).expect("the effects render"))
}
#[test]
fn a_gray_luminance_mask_shows_the_content_at_its_gray_level() {
let content = Item::new_from_element(Raster::new_cpu(Image::new(1, 1, Color::WHITE)));
@@ -115,15 +121,24 @@ mod test {
0_f64.into(),
invert.into(),
);
raster_kernels::evaluate_raster_effects(result).into_element().data[0].a()
rendered(result).map(|rendered| rendered.element().data[0].a())
};
// Luminance is taken on gamma-encoded channels, so a middle gray shows half the content, as a PSD mask's gray level does
assert!((masked(MaskMode::Luminance, 100., false) - 0.5).abs() < 1e-3);
assert!((masked(MaskMode::Luminance, 100., true) - 0.5).abs() < 1e-3);
assert!((masked(MaskMode::Luminance, 50., false) - 0.75).abs() < 1e-3, "half the density hides half as much");
assert!((masked(MaskMode::Alpha, 100., false) - 1.).abs() < 1e-6, "an opaque mask shows everything by its alpha");
assert!(masked(MaskMode::Alpha, 100., true) < 1e-6);
let cases = [
(MaskMode::Luminance, 100., false, 0.5),
(MaskMode::Luminance, 100., true, 0.5),
(MaskMode::Luminance, 50., false, 0.75),
(MaskMode::Alpha, 100., false, 1.),
(MaskMode::Alpha, 100., true, 0.),
];
for (mode, density, invert, expected) in cases {
let Some(alpha) = masked(mode, density, invert) else { return };
assert!(
(alpha - expected).abs() < 0.005,
"{mode:?} at a density of {density} (inverted: {invert}) shows {alpha} rather than {expected}"
);
}
}
#[test]
@@ -156,7 +171,8 @@ mod test {
let mirror = DAffine2::from_translation(DVec2::new(4., 0.)) * DAffine2::from_scale(DVec2::new(-1., 1.));
masked.set_attribute(ATTR_TRANSFORM, mirror * placement);
let alphas: Vec<f32> = raster_kernels::evaluate_raster_effects(masked).into_element().data.iter().map(|color| color.a().round()).collect();
let Some(rendered) = rendered(masked) else { return };
let alphas: Vec<f32> = rendered.element().data.iter().map(|color| color.a().round()).collect();
assert_eq!(alphas, [0., 0., 1., 1.], "the mask hides the half of the image it covered before the transform");
}
}
@@ -332,7 +332,7 @@ mod tests {
#[tokio::test]
async fn rasterize_renders_the_footprint_area_at_its_resolution() {
let Some(executor) = ::wgpu_executor::WgpuExecutor::new().await else { return };
let Some(executor) = ::wgpu_executor::WgpuExecutor::for_tests().await else { return };
let red = Item::new_from_element(Raster::new_cpu(Image::new(1, 1, Color::RED))).with_attribute(ATTR_TRANSFORM, DAffine2::from_scale(DVec2::new(500., 1000.)));
let data = List::new_from_item(Item::new_from_element(Graphic::RasterCPU(Box::new(red))));
+15 -5
View File
@@ -793,7 +793,9 @@ mod tests {
#[test]
fn regions_out_of_view_are_held_weakly_and_those_in_view_strongly() {
let Some(context) = futures::executor::block_on(wgpu_executor::WgpuContext::new()) else { return };
let Some(context) = futures::executor::block_on(wgpu_executor::WgpuContext::for_tests()) else {
return;
};
let cache = TileCache::default();
let mut pool = Vec::new();
@@ -808,7 +810,9 @@ mod tests {
#[test]
fn a_parked_region_back_in_view_is_reused() {
let Some(context) = futures::executor::block_on(wgpu_executor::WgpuContext::new()) else { return };
let Some(context) = futures::executor::block_on(wgpu_executor::WgpuContext::for_tests()) else {
return;
};
let cache = TileCache::default();
let (_, parked) = show(&cache, &context.device, &view(0), &zoom(1));
@@ -819,7 +823,9 @@ mod tests {
#[test]
fn a_parked_region_the_pool_evicted_is_rendered_again() {
let Some(context) = futures::executor::block_on(wgpu_executor::WgpuContext::new()) else { return };
let Some(context) = futures::executor::block_on(wgpu_executor::WgpuContext::for_tests()) else {
return;
};
let cache = TileCache::default();
let (_, evicted) = show(&cache, &context.device, &view(0), &zoom(1));
@@ -833,7 +839,9 @@ mod tests {
#[test]
fn the_regions_of_an_earlier_cache_key_are_reused_on_returning_to_it() {
let Some(context) = futures::executor::block_on(wgpu_executor::WgpuContext::new()) else { return };
let Some(context) = futures::executor::block_on(wgpu_executor::WgpuContext::for_tests()) else {
return;
};
let cache = TileCache::default();
let (_, parked) = show(&cache, &context.device, &view(0), &zoom(1));
@@ -844,7 +852,9 @@ mod tests {
#[test]
fn the_regions_of_cache_keys_older_than_the_most_recent_ones_are_released() {
let Some(context) = futures::executor::block_on(wgpu_executor::WgpuContext::new()) else { return };
let Some(context) = futures::executor::block_on(wgpu_executor::WgpuContext::for_tests()) else {
return;
};
let cache = TileCache::default();
let (_, oldest) = show(&cache, &context.device, &view(0), &zoom(0));
@@ -250,7 +250,7 @@ mod tests {
#[test]
fn an_svg_render_is_partial_until_its_readback_lands() {
let Some(executor) = futures::executor::block_on(WgpuExecutor::new()) else { return };
let Some(executor) = futures::executor::block_on(WgpuExecutor::for_tests()) else { return };
let tasks = Arc::new(HeldTasks::default());
let runtime = AsyncRuntime::new(tasks.clone());
let slot = SvgReadbackSlot::default();
@@ -301,7 +301,7 @@ mod tests {
#[test]
fn evaluations_of_content_in_view_draw_the_same_placeholders_so_its_readback_finishes_them() {
let Some(executor) = futures::executor::block_on(WgpuExecutor::new()) else { return };
let Some(executor) = futures::executor::block_on(WgpuExecutor::for_tests()) else { return };
let tasks = Arc::new(HeldTasks::default());
let runtime = AsyncRuntime::new(tasks.clone());
let (slot, in_view) = (SvgReadbackSlot::default(), SvgInViewSlot::default());
@@ -330,7 +330,7 @@ mod tests {
#[test]
fn a_render_for_another_view_cancels_the_readback_of_the_one_it_replaces() {
let Some(executor) = futures::executor::block_on(WgpuExecutor::new()) else { return };
let Some(executor) = futures::executor::block_on(WgpuExecutor::for_tests()) else { return };
let tasks = Arc::new(HeldTasks::default());
let runtime = AsyncRuntime::new(tasks.clone());
let slot = SvgReadbackSlot::default();
@@ -355,7 +355,7 @@ mod tests {
#[test]
fn a_readback_underway_is_cancelled_once_its_slot_is_dropped() {
let Some(executor) = futures::executor::block_on(WgpuExecutor::new()) else { return };
let Some(executor) = futures::executor::block_on(WgpuExecutor::for_tests()) else { return };
let tasks = Arc::new(HeldTasks::default());
let runtime = AsyncRuntime::new(tasks.clone());
let slot = SvgReadbackSlot::default();
+9 -3
View File
@@ -1460,7 +1460,10 @@ mod tests {
let mut square = Item::new_from_element(Raster::new_cpu(Image::new(4, 4, Color::WHITE))).with_attribute(ATTR_TRANSFORM, DAffine2::from_scale(DVec2::splat(4.)));
stamp_effect(&mut square, Effect::new(&shadow));
let desaturated = raster_kernels::evaluate_raster_effects(desaturate((), square, DesaturateMethod::default().into()));
let Some(executor) = futures::executor::block_on(wgpu_executor::WgpuExecutor::for_tests()) else {
return;
};
let desaturated = futures::executor::block_on(executor.read_back(desaturate((), square, DesaturateMethod::default().into()))).expect("the effects render");
let shaded = desaturated.element().data.iter().filter(|pixel| pixel.a() > 0.5 && pixel.r() < 0.99).collect::<Vec<_>>();
assert!(!shaded.is_empty(), "the shadow was carried out");
@@ -2141,7 +2144,10 @@ mod tests {
use core_types::ATTR_TRANSFORM;
use glam::DAffine2;
use graphic_types::{Effect, EffectOperation, stamp_effect};
use raster_kernels::evaluate_raster_effects;
let Some(executor) = futures::executor::block_on(wgpu_executor::WgpuExecutor::for_tests()) else {
return;
};
// A white square in the middle of a transparent image, placed one pixel per document unit, then blurred
let mut image = Image::new(20, 20, Color::TRANSPARENT);
@@ -2160,7 +2166,7 @@ mod tests {
gamma: false,
}),
);
let result = evaluate_raster_effects(super::make_opaque((), item, Item::new_from_element(100.)));
let result = futures::executor::block_on(executor.read_back(super::make_opaque((), item, Item::new_from_element(100.)))).expect("the effects render");
// Just outside the square, the blur's faint white becomes opaque white, where making it opaque first would have blurred in black
let transform: DAffine2 = result.attribute_cloned_or_default(ATTR_TRANSFORM);
+1 -1
View File
@@ -223,7 +223,7 @@ mod tests {
fn rendered(graphic: &Item<Graphic>, size: UVec2) -> Option<Vec<[u8; 4]>> {
use rendering::{Render, RenderParams};
let executor = futures::executor::block_on(wgpu_executor::WgpuExecutor::new())?;
let executor = futures::executor::block_on(wgpu_executor::WgpuExecutor::for_tests())?;
let list = core_types::list::List::new_from_item(graphic.clone());
let draw = &mut |scene: &mut rendering::vello_scene_builder::VelloSceneBuilder| list.render_to_vello(scene, DAffine2::IDENTITY, &RenderParams::default());
let texture = executor.render_vello(size, DAffine2::IDENTITY, draw).expect("the graphic should render");
@@ -140,7 +140,7 @@ struct Compiled {
impl Compiled {
/// The `network` preprocessed and compiled with a GPU in scope, or `None` without a GPU.
fn new(network: NodeNetwork) -> Option<Self> {
let gpu = futures::executor::block_on(wgpu_executor::WgpuExecutor::new())?;
let gpu = futures::executor::block_on(wgpu_executor::WgpuExecutor::for_tests())?;
let application_io = PlatformApplicationIo::new_with_executor(gpu);
Some(Self::with_application_io(network, application_io))
}