From 01c5455ecf9e3dee6562bd775320a2a208306d46 Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Fri, 4 Dec 2026 01:21:12 -0800 Subject: [PATCH] Fix the 'Clipping Mask' node not clipping endless color, gradient, and generator layers (#5072) --- .../portfolio/document/clipping_tests.rs | 109 ++++++++++++++++++ .../libraries/rendering/src/renderer.rs | 21 ++-- .../wgpu-executor/src/vello_render/tests.rs | 40 +++++++ 3 files changed, 160 insertions(+), 10 deletions(-) diff --git a/editor/src/messages/portfolio/document/clipping_tests.rs b/editor/src/messages/portfolio/document/clipping_tests.rs index beea9d273d..1f5dc64b19 100644 --- a/editor/src/messages/portfolio/document/clipping_tests.rs +++ b/editor/src/messages/portfolio/document/clipping_tests.rs @@ -5,10 +5,14 @@ use crate::test_utils::gpu_executor; use crate::test_utils::test_prelude::*; use glam::{DAffine2, UVec2}; use graph_craft::document::value::TaggedValue; +use graphene_std::application_io::Texture; use graphene_std::blending_nodes::effects::{blur, drop_shadow}; use graphene_std::graphic::{IntoGraphicList, extend}; use graphene_std::list::{Item, List}; +use graphene_std::math_nodes::{color_value, gradient_value}; +use graphene_std::raster_nodes::std_nodes::noise_pattern; use graphene_std::renderer::{Render, RenderParams, RenderSvgSegmentList, SvgRender}; +use graphene_std::vector::style::RenderMode; use graphene_std::{Graphic, NodeParameter, ProtoNodeIdentifier, Vector}; #[derive(Clone, Copy, Debug)] @@ -136,6 +140,111 @@ fn assert_clip(name: &str, graphic: &Graphic, blurred: bool) { assert_eq!(pixel(170, 90)[3], 0, "{name}: the red is clipped away well past the base"); } +/// Endless content a layer can hold, which fills the whole canvas unless something clips it. +#[derive(Clone, Copy, Debug)] +enum Endless { + Color, + Gradient, + NoisePattern, +} + +/// A document viewed in the `render_mode` with a blue rectangle filling (50, 50) to (130, 130). +async fn document_with_a_rectangle(render_mode: RenderMode) -> EditorTestUtils { + let mut editor = EditorTestUtils::create(); + editor.new_document().await; + editor.handle_message(DocumentMessage::SetRenderMode { render_mode }).await; + + let blue = Color::from_rgbaf32_unchecked(0., 0., 1., 1.); + editor.select_primary_color(blue).await; + editor.select_secondary_color(blue).await; + editor.draw_rect(50., 50., 130., 130.).await; + + editor +} + +/// Adds a new layer of `endless` content above the selected layer, clipped to it as by an Alt-click between them. +async fn add_clipped_endless_layer(editor: &mut EditorTestUtils, endless: Endless) { + editor.handle_message(DocumentMessage::CreateEmptyFolder).await; + let layer = editor.get_selected_layer().await.expect("the new layer is selected"); + let node = match endless { + Endless::Color => color_value::IDENTIFIER, + Endless::Gradient => gradient_value::IDENTIFIER, + Endless::NoisePattern => noise_pattern::IDENTIFIER, + }; + let node_type = DefinitionIdentifier::ProtoNode(node); + editor.handle_message(NodeGraphMessage::CreateNodeInLayerWithTransaction { node_type, layer }).await; + editor.handle_message(DocumentMessage::ClipLayer { id: layer.to_node() }).await; +} + +/// Evaluates the document for a viewport of `size` as the app does, returning what the frontend is sent and the frame the runtime presents. +async fn evaluate_viewport(editor: &mut EditorTestUtils, size: UVec2) -> (Vec, Option) { + let portfolio = editor.portfolio_message_handler_mut(); + let document_id = portfolio.active_document_id.expect("a document is open"); + let (executor, documents) = (&mut portfolio.executor, &mut portfolio.documents); + let document = documents.get_mut(&document_id).expect("the active document exists"); + let submitted = executor + .submit_node_graph_evaluation(document, document_id, size, 1., Default::default(), Vec::new(), true, DVec2::ZERO) + .expect("the evaluation is submitted"); + + let mut messages = editor.dispatch(submitted); + editor.runtime.update(); + let frame = editor.runtime.run().await; + messages.extend(editor.dispatch(Message::Wake)); + + (messages, frame) +} + +#[tokio::test] +async fn endless_layers_clip_to_the_shape_beneath_them() { + let size = UVec2::new(256, 256); + + let frame_pixels = async |editor: &mut EditorTestUtils| { + let (_, frame) = evaluate_viewport(editor, size).await; + let frame = frame.expect("the viewport presents a frame"); + gpu_executor().read_texture_pixels(&frame).expect("the frame reads back") + }; + let pixel = |pixels: &[u8], x: u32, y: u32| { + let index = (y * size.x + x) as usize * 4; + [pixels[index], pixels[index + 1], pixels[index + 2], pixels[index + 3]] + }; + + for endless in [Endless::Color, Endless::Gradient, Endless::NoisePattern] { + // In the viewport's Vello render, the endless content covers the rectangle but leaves the canvas past it as the rectangle alone left it + let mut editor = document_with_a_rectangle(RenderMode::Normal).await; + let rectangle_alone = frame_pixels(&mut editor).await; + add_clipped_endless_layer(&mut editor, endless).await; + let clipped = frame_pixels(&mut editor).await; + + assert_ne!(pixel(&clipped, 90, 90), pixel(&rectangle_alone, 90, 90), "{endless:?}: the content shows within the rectangle"); + for (x, y) in [(20, 20), (200, 90), (90, 200), (230, 230)] { + assert_eq!( + pixel(&clipped, x, y), + pixel(&rectangle_alone, x, y), + "{endless:?}: the content is clipped away at ({x}, {y}), past the rectangle" + ); + } + + // In SVG Preview, the endless content is drawn only within the rectangle's mask + let mut editor = document_with_a_rectangle(RenderMode::SvgPreview).await; + add_clipped_endless_layer(&mut editor, endless).await; + let (messages, _) = evaluate_viewport(&mut editor, size).await; + let svg = messages + .into_iter() + .find_map(|message| match message { + FrontendMessage::UpdateDocumentArtwork { svg } => Some(svg), + _ => None, + }) + .expect("the viewport's artwork is sent as SVG"); + + let (unmasked, masked) = svg.split_once(r#"mask="url(#mask-"#).unwrap_or_else(|| panic!("{endless:?}: the content is masked: {svg}")); + let draws_endless_content = |markup: &str| ["( *mask_element_and_transform = None; } - if let Some(rect) = layer_rect { - scene.push_layer(peniko::Fill::NonZero, peniko::Mix::Normal, 1., kurbo::Affine::IDENTITY, &rect); + // Content without edges, like a color layer, is clipped in layers covering the whole render + let push_layer = |scene: &mut VelloSceneBuilder, blend_mode: peniko::BlendMode| match layer_rect { + Some(rect) => scene.push_layer(peniko::Fill::NonZero, blend_mode, 1., kurbo::Affine::IDENTITY, &rect), + None => scene.push_viewport_layer(blend_mode, 1.), + }; + let clipped = layer_rect.is_some() || bounds == RenderBoundingBox::Infinite; + + if clipped { + push_layer(scene, peniko::Mix::Normal.into()); mask_element.render_to_vello(scene, transform_mask, &render_params.for_clipper()); - scene.push_layer( - peniko::Fill::NonZero, - peniko::BlendMode::new(peniko::Mix::Normal, peniko::Compose::SrcIn), - 1., - kurbo::Affine::IDENTITY, - &rect, - ); + push_layer(scene, peniko::BlendMode::new(peniko::Mix::Normal, peniko::Compose::SrcIn)); } draw_content(scene); - if layer_rect.is_some() { + if clipped { scene.pop_layer(); scene.pop_layer(); } diff --git a/node-graph/libraries/wgpu-executor/src/vello_render/tests.rs b/node-graph/libraries/wgpu-executor/src/vello_render/tests.rs index a371bfc545..16e496d17d 100644 --- a/node-graph/libraries/wgpu-executor/src/vello_render/tests.rs +++ b/node-graph/libraries/wgpu-executor/src/vello_render/tests.rs @@ -1376,6 +1376,46 @@ fn a_clipping_base_clips_by_its_blur_but_not_by_its_shadow() { } } +#[test] +fn endless_layers_clip_to_the_shape_beneath_them() { + use core_types::list::{ATTR_APPEARANCE, Item, List}; + use core_types::{ATTR_CLIPPING_MASK, Color as GraphicColor}; + use graphic_types::generator::Generator; + use graphic_types::vector_types::gradient::Gradient; + use graphic_types::{Appearance, Coverage, Graphic, Vector}; + use rendering::{Render, RenderParams}; + use vello_gpu::kurbo::Shape; + + let size = UVec2::new(128, 128); + let mut square = Vector::item_from_bezpath(&Rect::new(30., 30., 80., 80.).to_path(0.1)); + square.set_attribute( + ATTR_APPEARANCE, + Appearance::new_single(Coverage::new_fill(), Graphic::ColorList(List::new_from_element(GraphicColor::WHITE))), + ); + let base = Item::new_from_element(Graphic::VectorList(List::new_from_item(square))); + + // Layers of a color, a gradient, and noise, each clipped to a square beneath it + let layers = [ + ("color", Graphic::from(Item::new_from_element(GraphicColor::RED).with_attribute(ATTR_CLIPPING_MASK, true))), + ( + "gradient", + Graphic::from(Item::new_from_element(Gradient::from(vec![GraphicColor::RED, GraphicColor::BLUE])).with_attribute(ATTR_CLIPPING_MASK, true)), + ), + ("noise", Graphic::from(Item::new_from_element(Generator::Noise).with_attribute(ATTR_CLIPPING_MASK, true))), + ]; + for (name, layer) in layers { + let graphic = Graphic::GraphicList([base.clone(), Item::new_from_element(layer)].into_iter().collect()); + let Some(pixels) = render(size, |scene| graphic.render_to_vello(scene, DAffine2::IDENTITY, &RenderParams::default())) else { + return; + }; + + assert_eq!(pixel(&pixels, size, 55, 55)[3], 255, "{name}: the layer shows within the square"); + for (x, y) in [(10, 10), (100, 55), (55, 100), (120, 120)] { + assert_eq!(pixel(&pixels, size, x, y)[3], 0, "{name}: the layer is clipped away at ({x}, {y}), past the square"); + } + } +} + #[test] fn feathers_fade_the_content_to_nothing_at_its_edges() { use graphic_types::EffectOperation;