From 56dbb31d146ec8121b5bcb3ffd48561802e9392b Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Wed, 14 Oct 2026 06:42:15 -0700 Subject: [PATCH 1/2] Add shadow and glow nodes and make the 'Blur' node work on any graphic --- .branding | 4 +- Cargo.lock | 4 + desktop/src/render/state.rs | 4 +- desktop/wrapper/src/messages.rs | 2 +- .../nodes/src/blending_nodes/mod.rs | 3 + .../nodes/src/blending_nodes/renames.rs | 5 + document/migrations/nodes/src/lib.rs | 1 + .../data_panel/data_panel_message_handler.rs | 45 +- .../portfolio/document/document_message.rs | 3 + .../document/document_message_handler.rs | 8 + .../document/overlays/utility_types_native.rs | 6 +- .../utility_types/document_metadata.rs | 14 +- .../network_interface/queries.rs | 18 +- .../network_interface/structure.rs | 5 + .../messages/portfolio/document_migration.rs | 4 +- editor/src/node_graph_executor.rs | 10 +- frontend/src/icons.ts | 12 +- node-graph/libraries/core-types/src/lib.rs | 7 +- node-graph/libraries/core-types/src/list.rs | 3 + .../libraries/graphic-types/src/effects.rs | 275 ++++++ node-graph/libraries/graphic-types/src/lib.rs | 2 + .../raster-kernels/src/effect_kernels.rs | 343 +++++++ .../libraries/raster-kernels/src/lib.rs | 3 + .../raster-kernels/src/raster_effects.rs | 202 ++++ node-graph/libraries/rendering/Cargo.toml | 1 + .../libraries/rendering/src/renderer.rs | 873 +++++++++++++----- .../rendering/src/vello_scene_builder.rs | 90 +- node-graph/libraries/wgpu-executor/Cargo.toml | 2 + node-graph/libraries/wgpu-executor/src/lib.rs | 3 +- .../wgpu-executor/src/vello_effects.rs | 592 ++++++++++++ .../wgpu-executor/src/vello_effects.wgsl | 147 +++ .../wgpu-executor/src/vello_render.rs | 173 ++-- .../wgpu-executor/src/vello_render/tests.rs | 247 +++++ node-graph/nodes/blending/src/effects.rs | 148 +++ node-graph/nodes/blending/src/lib.rs | 2 + node-graph/nodes/gstd/Cargo.toml | 1 + node-graph/nodes/gstd/src/lib.rs | 2 +- .../nodes/gstd/src/platform_application_io.rs | 2 +- node-graph/nodes/raster/src/adjustments.rs | 2 +- node-graph/nodes/raster/src/dehaze.rs | 3 +- node-graph/nodes/raster/src/filter.rs | 269 +----- .../nodes/raster/src/image_color_palette.rs | 2 + node-graph/nodes/raster/src/std_nodes.rs | 116 ++- node-graph/nodes/vectorize/src/lib.rs | 2 +- 44 files changed, 3042 insertions(+), 618 deletions(-) create mode 100644 document/migrations/nodes/src/blending_nodes/mod.rs create mode 100644 document/migrations/nodes/src/blending_nodes/renames.rs create mode 100644 node-graph/libraries/graphic-types/src/effects.rs create mode 100644 node-graph/libraries/raster-kernels/src/effect_kernels.rs create mode 100644 node-graph/libraries/raster-kernels/src/raster_effects.rs create mode 100644 node-graph/libraries/wgpu-executor/src/vello_effects.rs create mode 100644 node-graph/libraries/wgpu-executor/src/vello_effects.wgsl create mode 100644 node-graph/nodes/blending/src/effects.rs diff --git a/.branding b/.branding index 6cdf622c162..1b6f97a87ab 100644 --- a/.branding +++ b/.branding @@ -1,2 +1,2 @@ -https://github.com/Keavon/graphite-branded-assets/archive/70b260c21afad29140bbc63ddab952fec3dd024c.tar.gz -5b05e7f4449b5f962755542e9b156891bbf2e0a87b7fc90798b8352d9225757d +https://github.com/Keavon/graphite-branded-assets/archive/38176c494d7cf0bde3bdf45e6a318ae6a9d89229.tar.gz +2ae24506ff786b2b684f5d23bd326a69f5f4551855f19e4ef2b0324f9ec14e5d diff --git a/Cargo.lock b/Cargo.lock index bdc86698072..ab342de6a0d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2664,6 +2664,7 @@ dependencies = [ "math-nodes", "node-macro", "path-bool-nodes", + "raster-kernels", "raster-nodes", "rendering", "repeat-nodes", @@ -6011,6 +6012,7 @@ dependencies = [ "usvg", "vector-types", "vello_gpu", + "wgpu", ] [[package]] @@ -8538,8 +8540,10 @@ dependencies = [ "futures", "glam", "graphene-application-io", + "graphic-types", "log", "node-macro", + "raster-kernels", "raster-types", "rendering", "thiserror 2.0.21", diff --git a/desktop/src/render/state.rs b/desktop/src/render/state.rs index 00e2ae81c0e..4936fa2bfe4 100644 --- a/desktop/src/render/state.rs +++ b/desktop/src/render/state.rs @@ -21,7 +21,7 @@ pub(crate) struct RenderState { ui_texture: Option, bind_group: Option, #[derivative(Debug = "ignore")] - overlays_scene: Option, + overlays_scene: Option>, #[derivative(Debug = "ignore")] overlays_canvas: VelloCanvas, surface_outdated: bool, @@ -220,7 +220,7 @@ impl RenderState { self.viewport_offset = offset; } - pub(crate) fn set_overlays_scene(&mut self, scene: VelloSceneBuilder) { + pub(crate) fn set_overlays_scene(&mut self, scene: VelloSceneBuilder<'static>) { self.surface_outdated = true; self.overlays_scene = Some(scene); } diff --git a/desktop/wrapper/src/messages.rs b/desktop/wrapper/src/messages.rs index f5691c57a80..19a9d255c45 100644 --- a/desktop/wrapper/src/messages.rs +++ b/desktop/wrapper/src/messages.rs @@ -40,7 +40,7 @@ pub enum DesktopFrontendMessage { WindowUpdateDirectInput { enabled: bool, }, - UpdateOverlays(Box), + UpdateOverlays(Box>), PersistenceUpdatePreferences { preferences: Preferences, }, diff --git a/document/migrations/nodes/src/blending_nodes/mod.rs b/document/migrations/nodes/src/blending_nodes/mod.rs new file mode 100644 index 00000000000..8a4e8d1ba4e --- /dev/null +++ b/document/migrations/nodes/src/blending_nodes/mod.rs @@ -0,0 +1,3 @@ +//! Migrations of the nodes in `blending_nodes`. + +mod renames; diff --git a/document/migrations/nodes/src/blending_nodes/renames.rs b/document/migrations/nodes/src/blending_nodes/renames.rs new file mode 100644 index 00000000000..f0e9e13854d --- /dev/null +++ b/document/migrations/nodes/src/blending_nodes/renames.rs @@ -0,0 +1,5 @@ +//! Nodes that only moved to a new identifier. + +use migration_core::rename; + +rename!("raster_nodes::filter::BlurNode"@1 => "blending_nodes::effects::BlurNode"@2); diff --git a/document/migrations/nodes/src/lib.rs b/document/migrations/nodes/src/lib.rs index a72966d217f..3b6523a41ac 100644 --- a/document/migrations/nodes/src/lib.rs +++ b/document/migrations/nodes/src/lib.rs @@ -1,6 +1,7 @@ //! Node migrations, in a module per node crate and a file per step, each registering itself. Scaffold one with //! `cargo run migration new ` after bumping a node's `version(N)`. +mod blending_nodes; mod graphene_core; mod math_nodes; mod raster_nodes; diff --git a/editor/src/messages/portfolio/document/data_panel/data_panel_message_handler.rs b/editor/src/messages/portfolio/document/data_panel/data_panel_message_handler.rs index 4cea6462595..8a53fae5bbe 100644 --- a/editor/src/messages/portfolio/document/data_panel/data_panel_message_handler.rs +++ b/editor/src/messages/portfolio/document/data_panel/data_panel_message_handler.rs @@ -32,7 +32,7 @@ use graphene_std::vector::style::{ DashPattern, FillChoice, GradientForm, GradientHueDirection, GradientInterpolation, GradientRamp, GradientSettings, GradientSpace, GradientSpread, StrokeAlign, StrokeCap, StrokeJoin, }; use graphene_std::vector::{CurveType, Path, PointId, QRCodeErrorCorrectionLevel, SegmentId, Vector}; -use graphene_std::{Appearance, Artboard, Color, ContextFeatures, ContextOverrides, Cover, Coverage, Graphic}; +use graphene_std::{Appearance, Artboard, Color, ContextFeatures, ContextOverrides, Cover, Coverage, Effect, EffectKind, Effects, Graphic}; use kurbo::BezPath; use std::any::Any; use std::ops::Range; @@ -792,6 +792,45 @@ impl TableItemLayout for Coverage { } } +impl TableItemLayout for Effects { + fn type_name() -> &'static str { + "Effects" + } + fn identifier(&self) -> String { + "Effects".to_string() + } + // The wrapping `Item` already contributes the breadcrumb; the inner list supplies the next level + fn layout_with_breadcrumb(&self, data: &mut LayoutData) -> Vec { + self.value_page(data) + } + // Label the spreadsheet's element button with the inner list's identifier, like Appearance + fn value_widgets(&self, target: PathStep, data: &LayoutData) -> Vec { + self.0.value_widgets(target, data) + } + fn value_page(&self, data: &mut LayoutData) -> Vec { + self.0.layout_with_breadcrumb(data) + } +} + +impl TableItemLayout for Effect { + fn type_name() -> &'static str { + "Effect" + } + fn identifier(&self) -> String { + "Effect".to_string() + } + // The wrapping row already contributes the breadcrumb; the inner item supplies the next level + fn layout_with_breadcrumb(&self, data: &mut LayoutData) -> Vec { + self.value_page(data) + } + fn value_widgets(&self, target: PathStep, data: &LayoutData) -> Vec { + self.0.value_widgets(target, data) + } + fn value_page(&self, data: &mut LayoutData) -> Vec { + self.0.layout_with_breadcrumb(data) + } +} + impl TableItemLayout for TransferCurve { fn type_name() -> &'static str { "TransferCurve" @@ -1213,6 +1252,7 @@ macro_rules! impl_table_item_layout_for_choice_enum { impl_table_item_layout_for_choice_enum!( BlendMode, Cover, + EffectKind, GradientForm, GradientSpread, GradientSpace, @@ -1383,6 +1423,9 @@ macro_rules! known_item_types { Appearance, Coverage, Cover, + Effects, + Effect, + EffectKind, DashPattern, BoxCorners, TransferCurve, diff --git a/editor/src/messages/portfolio/document/document_message.rs b/editor/src/messages/portfolio/document/document_message.rs index 1b4105b113e..16b671e6aab 100644 --- a/editor/src/messages/portfolio/document/document_message.rs +++ b/editor/src/messages/portfolio/document/document_message.rs @@ -325,6 +325,9 @@ pub enum DocumentMessage { UpdateAppearanceAttributes { appearance_attributes: HashMap>, }, + UpdateEffectsReach { + effects_reach: HashMap, + }, Undo, UngroupSelectedLayers, UngroupLayer { diff --git a/editor/src/messages/portfolio/document/document_message_handler.rs b/editor/src/messages/portfolio/document/document_message_handler.rs index b5babf92b71..8036ce0a820 100644 --- a/editor/src/messages/portfolio/document/document_message_handler.rs +++ b/editor/src/messages/portfolio/document/document_message_handler.rs @@ -1915,6 +1915,14 @@ impl MessageHandler> for DocumentMes .collect(); self.network_interface.update_appearance_attributes(layer_appearance_attributes); } + DocumentMessage::UpdateEffectsReach { effects_reach } => { + let layer_effects_reach = effects_reach + .into_iter() + .filter(|(node_id, _)| self.network_interface.document_network().nodes.contains_key(node_id) && self.network_interface.is_layer(node_id, &[])) + .map(|(node_id, reach)| (LayerNodeIdentifier::new(node_id, &self.network_interface), reach)) + .collect(); + self.network_interface.update_effects_reach(layer_effects_reach); + } DocumentMessage::Undo => { if self.network_interface.transaction_status() != TransactionStatus::Finished { return; diff --git a/editor/src/messages/portfolio/document/overlays/utility_types_native.rs b/editor/src/messages/portfolio/document/overlays/utility_types_native.rs index 852e2368377..1c810670a21 100644 --- a/editor/src/messages/portfolio/document/overlays/utility_types_native.rs +++ b/editor/src/messages/portfolio/document/overlays/utility_types_native.rs @@ -270,7 +270,7 @@ impl OverlayContext { } } - pub fn take_scene(self) -> VelloSceneBuilder { + pub fn take_scene(self) -> VelloSceneBuilder<'static> { let mut internal = self.internal.lock().expect("Failed to lock internal overlay context"); let empty_scene = overlay_scene(&internal.viewport); std::mem::replace(&mut internal.scene, empty_scene) @@ -486,13 +486,13 @@ pub enum DrawHandles { } pub(super) struct OverlayContextInternal { - scene: VelloSceneBuilder, + scene: VelloSceneBuilder<'static>, viewport: ViewportMessageHandler, visibility_settings: OverlaysVisibilitySettings, } /// An empty scene covering the viewport's physical pixels, matching the resolution the viewport renders at. -fn overlay_scene(viewport: &ViewportMessageHandler) -> VelloSceneBuilder { +fn overlay_scene(viewport: &ViewportMessageHandler) -> VelloSceneBuilder<'static> { // The smallest texture size limit WebGL2 guarantees, which only matters for the overlays' tiny pattern images const MAX_TEXTURE_SIZE: u32 = 2048; diff --git a/editor/src/messages/portfolio/document/utility_types/document_metadata.rs b/editor/src/messages/portfolio/document/utility_types/document_metadata.rs index d4566965378..f190638a3ab 100644 --- a/editor/src/messages/portfolio/document/utility_types/document_metadata.rs +++ b/editor/src/messages/portfolio/document/utility_types/document_metadata.rs @@ -51,6 +51,8 @@ pub struct DocumentMetadata { pub layer_vector_data: HashMap>>, /// Per-layer `ATTR_APPEARANCE` attribute, exposed so message handlers can read paint information that lives on the list. pub layer_appearance_attributes: HashMap>, + /// How far each layer's effects paint past its content, in document pixels, for the layers with effects. + pub layer_effects_reach: HashMap, /// Transform from document space to viewport space. pub document_to_viewport: DAffine2, /// The transform from document space to viewport space that the latest render was made with, which is the one `upstream_footprints` include. @@ -347,9 +349,9 @@ impl DocumentMetadata { } /// Get the bounding box of the specified layer in document space, expanded to include the actual rendered - /// stroke geometry when the layer is a vector with a stroke style. Only the first item of a layer's vector data - /// is recorded, so the others count through the click-target-based bounds, without their strokes. - pub fn bounding_box_document_with_stroke(&self, layer: LayerNodeIdentifier) -> Option<[DVec2; 2]> { + /// stroke geometry when the layer is a vector with a stroke style, and how far its effects paint past it. Only the first item of a + /// layer's vector data is recorded, so the others count through the click-target-based bounds, without their strokes. + pub fn bounding_box_document_with_stroke_and_effects(&self, layer: LayerNodeIdentifier) -> Option<[DVec2; 2]> { let stroked = self.layer_vector_data.get(&layer).and_then(|item| { let stroke = self .layer_appearance_attributes @@ -359,8 +361,10 @@ impl DocumentMetadata { let paths = paths_from_item_attributes(item.as_ref()).unwrap_or_else(|| no_paths()); item.element().stroke_inclusive_bounding_box_with_transform(paths, self.transform_to_document(layer), stroke.as_ref()) }); + let [min, max] = [stroked, self.bounding_box_document(layer)].into_iter().flatten().reduce(Quad::combine_bounds)?; - [stroked, self.bounding_box_document(layer)].into_iter().flatten().reduce(Quad::combine_bounds) + let reach = self.layer_effects_reach.get(&layer).copied().unwrap_or_default(); + Some([min - reach, max + reach]) } /// Get the bounding box of the click target of the specified layer in viewport space @@ -732,7 +736,7 @@ mod tests { let later_item = ClickTarget::new_with_path(line(100., 110.), 0.); metadata.click_targets.insert(layer, vec![Arc::new(later_item)]); - let [min, max] = metadata.bounding_box_document_with_stroke(layer).unwrap(); + let [min, max] = metadata.bounding_box_document_with_stroke_and_effects(layer).unwrap(); assert_eq!((min.x, max.x), (0., 110.)); } diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/queries.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/queries.rs index 9f5fd8f6e62..4fb02ea6e9a 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/queries.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/queries.rs @@ -1063,11 +1063,11 @@ impl NodeNetworkInterface { Some(transformed.bounding_box()) } - /// Calculates the document bounds in document space, expanding vector layer bounds to include the rendered - /// stroke width. Used for export so the output canvas captures strokes that overflow the path geometry. - pub fn document_bounds_document_space_with_stroke(&self, include_artboards: bool) -> Option<[DVec2; 2]> { + /// Calculates the document bounds in document space, expanding layer bounds to include the rendered stroke width and + /// effects. Used for export so the output canvas captures strokes and shadows that overflow the path geometry. + pub fn document_bounds_document_space_with_stroke_and_effects(&self, include_artboards: bool) -> Option<[DVec2; 2]> { self.document_metadata.document_bounds(include_artboards, true, || { - self.combined_document_bounds(include_artboards, |metadata, layer| metadata.bounding_box_document_with_stroke(layer)) + self.combined_document_bounds(include_artboards, |metadata, layer| metadata.bounding_box_document_with_stroke_and_effects(layer)) }) } @@ -1084,17 +1084,17 @@ impl NodeNetworkInterface { .reduce(Quad::combine_bounds) } - /// Calculates the selected layer bounds in document space, expanding vector layer bounds to include the - /// rendered stroke width. Used for export so the output canvas captures strokes that overflow the path geometry. - pub fn selected_bounds_document_space_with_stroke(&self, include_artboards: bool, network_path: &[NodeId]) -> Option<[DVec2; 2]> { + /// Calculates the selected layer bounds in document space, expanding layer bounds to include the rendered stroke width + /// and effects. Used for export so the output canvas captures strokes and shadows that overflow the path geometry. + pub fn selected_bounds_document_space_with_stroke_and_effects(&self, include_artboards: bool, network_path: &[NodeId]) -> Option<[DVec2; 2]> { let Some(selected_nodes) = self.selected_nodes_in_nested_network(network_path) else { - log::error!("Could not get selected nodes in selected_bounds_document_space_with_stroke"); + log::error!("Could not get selected nodes in selected_bounds_document_space_with_stroke_and_effects"); return None; }; selected_nodes .selected_layers(&self.document_metadata) .filter(|&layer| include_artboards || !self.is_artboard(&layer.to_node(), &[])) - .filter_map(|layer| self.document_metadata.bounding_box_document_with_stroke(layer)) + .filter_map(|layer| self.document_metadata.bounding_box_document_with_stroke_and_effects(layer)) .reduce(Quad::combine_bounds) } diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/structure.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/structure.rs index f6bb3d14375..286e820885d 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/structure.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/structure.rs @@ -200,4 +200,9 @@ impl NodeNetworkInterface { pub fn update_appearance_attributes(&mut self, new_layer_appearance_attributes: HashMap>) { self.document_metadata.layer_appearance_attributes = new_layer_appearance_attributes; } + + /// Update how far each layer's effects paint past its content. + pub fn update_effects_reach(&mut self, new_layer_effects_reach: HashMap) { + self.document_metadata.layer_effects_reach = new_layer_effects_reach; + } } diff --git a/editor/src/messages/portfolio/document_migration.rs b/editor/src/messages/portfolio/document_migration.rs index af2a48cfe91..70dc94bdc3d 100644 --- a/editor/src/messages/portfolio/document_migration.rs +++ b/editor/src/messages/portfolio/document_migration.rs @@ -512,8 +512,8 @@ const NODE_REPLACEMENTS: &[NodeReplacement<'static>] = &[ ], }, NodeReplacement { - node: graphene_std::raster_nodes::filter::blur::IDENTIFIER, - aliases: &["graphene_raster_nodes::filter::BlurNode", "graphene_std::filter::BlurNode"], + node: graphene_std::blending_nodes::effects::blur::IDENTIFIER, + aliases: &["raster_nodes::filter::BlurNode", "graphene_raster_nodes::filter::BlurNode", "graphene_std::filter::BlurNode"], }, NodeReplacement { node: graphene_std::raster_nodes::adjustments::brightness_contrast::IDENTIFIER, diff --git a/editor/src/node_graph_executor.rs b/editor/src/node_graph_executor.rs index 983adaeab1f..8447510ab1f 100644 --- a/editor/src/node_graph_executor.rs +++ b/editor/src/node_graph_executor.rs @@ -448,11 +448,11 @@ impl NodeGraphExecutor { }; // Calculate the bounding box of the region to be exported (artboard bounds always contribute). - // `AllArtwork` and `Selection` expand vector layer bounds by the rendered stroke width so strokes - // drawn at render-time (without a `Solidify Stroke`) aren't clipped at the export canvas edge. + // `AllArtwork` and `Selection` expand layer bounds by the rendered stroke width and effects reach so strokes + // and shadows drawn at render-time (without a `Solidify Stroke`) aren't clipped at the export canvas edge. let bounds = match export_config.bounds { - ExportBounds::AllArtwork => document.network_interface.document_bounds_document_space_with_stroke(true), - ExportBounds::Selection => document.network_interface.selected_bounds_document_space_with_stroke(true, &[]), + ExportBounds::AllArtwork => document.network_interface.document_bounds_document_space_with_stroke_and_effects(true), + ExportBounds::Selection => document.network_interface.selected_bounds_document_space_with_stroke_and_effects(true, &[]), ExportBounds::Artboard(id) => document.metadata().bounding_box_document(id), } .ok_or_else(|| "No bounding box".to_string())?; @@ -1034,6 +1034,7 @@ impl NodeGraphExecutor { clip_targets, vector_data, appearance_attributes, + effects_reach, backgrounds: _, } = render_output.metadata; @@ -1050,6 +1051,7 @@ impl NodeGraphExecutor { responses.add(DocumentMessage::UpdateClipTargets { clip_targets }); responses.add(DocumentMessage::UpdateVectorData { vector_data }); responses.add(DocumentMessage::UpdateAppearanceAttributes { appearance_attributes }); + responses.add(DocumentMessage::UpdateEffectsReach { effects_reach }); responses.add(DocumentMessage::RenderScrollbars); responses.add(DocumentMessage::RenderRulers); responses.add(OverlaysMessage::Draw); diff --git a/frontend/src/icons.ts b/frontend/src/icons.ts index ba811e57dd4..01e9e0b95bd 100644 --- a/frontend/src/icons.ts +++ b/frontend/src/icons.ts @@ -121,8 +121,6 @@ import NodeAsInteger from "/../branding/assets/16x16-nodes/node-as-integer.svg"; import NodeAsNumber from "/../branding/assets/16x16-nodes/node-as-number.svg"; import NodeAsString from "/../branding/assets/16x16-nodes/node-as-string.svg"; import NodeAsVector from "/../branding/assets/16x16-nodes/node-as-vector.svg"; -import NodeVaryRotations from "/../branding/assets/16x16-nodes/node-assign-rotations.svg"; -import NodeVaryScales from "/../branding/assets/16x16-nodes/node-assign-scales.svg"; import NodeAverage from "/../branding/assets/16x16-nodes/node-average.svg"; import NodeBakeTransform from "/../branding/assets/16x16-nodes/node-bake-transform.svg"; import NodeBasicBrush from "/../branding/assets/16x16-nodes/node-basic-brush.svg"; @@ -175,6 +173,7 @@ import NodeDimensions from "/../branding/assets/16x16-nodes/node-dimensions.svg" import NodeDistance from "/../branding/assets/16x16-nodes/node-distance.svg"; import NodeDivide from "/../branding/assets/16x16-nodes/node-divide.svg"; import NodeDotProduct from "/../branding/assets/16x16-nodes/node-dot-product.svg"; +import NodeDropShadow from "/../branding/assets/16x16-nodes/node-drop-shadow.svg"; import NodeEllipse from "/../branding/assets/16x16-nodes/node-ellipse.svg"; import NodeEquals from "/../branding/assets/16x16-nodes/node-equals.svg"; import NodeEvaluateGradient from "/../branding/assets/16x16-nodes/node-evaluate-gradient.svg"; @@ -224,6 +223,7 @@ import NodeImageToChannels from "/../branding/assets/16x16-nodes/node-image-to-c import NodeImage from "/../branding/assets/16x16-nodes/node-image.svg"; import NodeIndexPoints from "/../branding/assets/16x16-nodes/node-index-points.svg"; import NodeIndices from "/../branding/assets/16x16-nodes/node-indices.svg"; +import NodeInnerShadow from "/../branding/assets/16x16-nodes/node-inner-shadow.svg"; import NodeInvertTransform from "/../branding/assets/16x16-nodes/node-invert-transform.svg"; import NodeInvert from "/../branding/assets/16x16-nodes/node-invert.svg"; import NodeIsNonzero from "/../branding/assets/16x16-nodes/node-is-nonzero.svg"; @@ -276,6 +276,7 @@ import NodeOffsetPoints from "/../branding/assets/16x16-nodes/node-offset-points import NodeOpacity from "/../branding/assets/16x16-nodes/node-opacity.svg"; import NodeOpenEnds from "/../branding/assets/16x16-nodes/node-open-ends.svg"; import NodeOriginsToPolyline from "/../branding/assets/16x16-nodes/node-origins-to-polyline.svg"; +import NodeOuterGlow from "/../branding/assets/16x16-nodes/node-outer-glow.svg"; import NodePackStrips from "/../branding/assets/16x16-nodes/node-pack-strips.svg"; import NodePassthrough from "/../branding/assets/16x16-nodes/node-passthrough.svg"; import NodePathIsClosed from "/../branding/assets/16x16-nodes/node-path-is-closed.svg"; @@ -380,10 +381,13 @@ import NodeTurtlePath from "/../branding/assets/16x16-nodes/node-turtle-path.svg import NodeUngroup from "/../branding/assets/16x16-nodes/node-ungroup.svg"; import NodeUnweld from "/../branding/assets/16x16-nodes/node-unweld.svg"; import NodeVaryFillColors from "/../branding/assets/16x16-nodes/node-vary-fill-colors.svg"; +import NodeVaryRotations from "/../branding/assets/16x16-nodes/node-vary-rotations.svg"; +import NodeVaryScales from "/../branding/assets/16x16-nodes/node-vary-scales.svg"; import NodeVaryStrokeColors from "/../branding/assets/16x16-nodes/node-vary-stroke-colors.svg"; import NodeVec2ToNumbers from "/../branding/assets/16x16-nodes/node-vec2-to-numbers.svg"; import NodeVec2Value from "/../branding/assets/16x16-nodes/node-vec2-value.svg"; import NodeVectorToPoints from "/../branding/assets/16x16-nodes/node-vector-to-points.svg"; +import NodeVectorize from "/../branding/assets/16x16-nodes/node-vectorize.svg"; import NodeVibrance from "/../branding/assets/16x16-nodes/node-vibrance.svg"; import NodeVoronoiCells from "/../branding/assets/16x16-nodes/node-voronoi-cells.svg"; import NodeWeld from "/../branding/assets/16x16-nodes/node-weld.svg"; @@ -459,6 +463,7 @@ export const ICON_16X16_NODES = { NodeDistance: { svg: NodeDistance, size: 16 }, NodeDivide: { svg: NodeDivide, size: 16 }, NodeDotProduct: { svg: NodeDotProduct, size: 16 }, + NodeDropShadow: { svg: NodeDropShadow, size: 16 }, NodeEllipse: { svg: NodeEllipse, size: 16 }, NodeEquals: { svg: NodeEquals, size: 16 }, NodeEvaluateGradient: { svg: NodeEvaluateGradient, size: 16 }, @@ -508,6 +513,7 @@ export const ICON_16X16_NODES = { NodeImageToChannels: { svg: NodeImageToChannels, size: 16 }, NodeIndexPoints: { svg: NodeIndexPoints, size: 16 }, NodeIndices: { svg: NodeIndices, size: 16 }, + NodeInnerShadow: { svg: NodeInnerShadow, size: 16 }, NodeInvert: { svg: NodeInvert, size: 16 }, NodeInvertTransform: { svg: NodeInvertTransform, size: 16 }, NodeIsNonzero: { svg: NodeIsNonzero, size: 16 }, @@ -560,6 +566,7 @@ export const ICON_16X16_NODES = { NodeOpacity: { svg: NodeOpacity, size: 16 }, NodeOpenEnds: { svg: NodeOpenEnds, size: 16 }, NodeOriginsToPolyline: { svg: NodeOriginsToPolyline, size: 16 }, + NodeOuterGlow: { svg: NodeOuterGlow, size: 16 }, NodePackStrips: { svg: NodePackStrips, size: 16 }, NodePassthrough: { svg: NodePassthrough, size: 16 }, NodePath: { svg: NodePath, size: 16 }, @@ -670,6 +677,7 @@ export const ICON_16X16_NODES = { NodeVec2ToNumbers: { svg: NodeVec2ToNumbers, size: 16 }, NodeVec2Value: { svg: NodeVec2Value, size: 16 }, NodeVectorToPoints: { svg: NodeVectorToPoints, size: 16 }, + NodeVectorize: { svg: NodeVectorize, size: 16 }, NodeVibrance: { svg: NodeVibrance, size: 16 }, NodeVoronoiCells: { svg: NodeVoronoiCells, size: 16 }, NodeWeld: { svg: NodeWeld, size: 16 }, diff --git a/node-graph/libraries/core-types/src/lib.rs b/node-graph/libraries/core-types/src/lib.rs index c1c59659248..f9e93aa84a3 100644 --- a/node-graph/libraries/core-types/src/lib.rs +++ b/node-graph/libraries/core-types/src/lib.rs @@ -32,9 +32,10 @@ pub use glam_ext::FallibleVec2Operations; pub use graphene_hash; pub use graphene_hash::CacheHash; pub use list::{ - ATTR_BACKGROUND, ATTR_BLEND_MODE, ATTR_CLIP, ATTR_CLIPPING_MASK, ATTR_DIMENSIONS, ATTR_EDITOR_CLICK_TARGET, ATTR_EDITOR_LAYER_PATH, ATTR_EDITOR_MERGED_LAYERS, ATTR_EDITOR_TEXT_FRAME, ATTR_END, - ATTR_FONT, ATTR_FONT_SIZE, ATTR_GRADIENT_CYCLIC, ATTR_GRADIENT_FORM, ATTR_GRADIENT_HUE_DIRECTION, ATTR_GRADIENT_INTERPOLATION, ATTR_GRADIENT_SPACE, ATTR_GRADIENT_SPREAD, ATTR_LETTER_SPACING, - ATTR_LETTER_TILT, ATTR_LINE_HEIGHT, ATTR_LOCATION, ATTR_MAX_HEIGHT, ATTR_MAX_WIDTH, ATTR_NAME, ATTR_OPACITY, ATTR_OPACITY_FILL, ATTR_START, ATTR_TEXT_ALIGN, ATTR_TRANSFORM, ATTR_TYPE, + ATTR_BACKGROUND, ATTR_BLEND_MODE, ATTR_CLIP, ATTR_CLIPPING_MASK, ATTR_DIMENSIONS, ATTR_EDITOR_CLICK_TARGET, ATTR_EDITOR_LAYER_PATH, ATTR_EDITOR_MERGED_LAYERS, ATTR_EDITOR_TEXT_FRAME, + ATTR_EFFECTS, ATTR_END, ATTR_FONT, ATTR_FONT_SIZE, ATTR_GRADIENT_CYCLIC, ATTR_GRADIENT_FORM, ATTR_GRADIENT_HUE_DIRECTION, ATTR_GRADIENT_INTERPOLATION, ATTR_GRADIENT_SPACE, ATTR_GRADIENT_SPREAD, + ATTR_LETTER_SPACING, ATTR_LETTER_TILT, ATTR_LINE_HEIGHT, ATTR_LOCATION, ATTR_MAX_HEIGHT, ATTR_MAX_WIDTH, ATTR_NAME, ATTR_OPACITY, ATTR_OPACITY_FILL, ATTR_START, ATTR_TEXT_ALIGN, ATTR_TRANSFORM, + ATTR_TYPE, }; pub use memo::MemoHash; pub use num_traits; diff --git a/node-graph/libraries/core-types/src/list.rs b/node-graph/libraries/core-types/src/list.rs index cdb63d32087..510df7f96d0 100644 --- a/node-graph/libraries/core-types/src/list.rs +++ b/node-graph/libraries/core-types/src/list.rs @@ -80,6 +80,9 @@ pub const ATTR_POSITION: &str = "position"; pub const ATTR_MIDPOINT: &str = "midpoint"; /// Item's ordered list of paint passes, of type `Appearance`. Earlier coverages paint first, compositing below later ones. pub const ATTR_APPEARANCE: &str = "appearance"; +/// Item's ordered list of effects, of type `Effects`, which the renderer applies in turn to the pixels the item renders to. +/// Unlike the appearance, it doesn't cascade to descendants. +pub const ATTR_EFFECTS: &str = "effects"; // TODO: Add a "fill_rule" attribute as a sibling of "paint" on the coverage list (uniform across covers) once a FillRule type ships /// Coverage's `Graphic` paint (implicit default `Graphic::None`, painting nothing), on the /// `List` inside an `Appearance`. diff --git a/node-graph/libraries/graphic-types/src/effects.rs b/node-graph/libraries/graphic-types/src/effects.rs new file mode 100644 index 00000000000..5090b1b8ff3 --- /dev/null +++ b/node-graph/libraries/graphic-types/src/effects.rs @@ -0,0 +1,275 @@ +//! An item's effects: an ordered list of image operations on the pixels the item renders to, stored in the `ATTR_EFFECTS` attribute. +//! Each row is an [`EffectKind`] whose parameters ride its item as attributes, read as their defaults when absent. +//! Each renderer decodes the rows into [`EffectOperation`]s and carries them out its own way, at the resolution of each render. + +use core_types::Color; +use core_types::color::SRGBA8; +use core_types::graphene_hash::CacheHash; +use core_types::list::{ATTR_EFFECTS, Item, List}; +use glam::DVec2; +use std::fmt::Debug; + +/// The image operation an [`Effect`] carries out. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, CacheHash)] +pub enum EffectKind { + /// Softens the content by blurring it. + #[default] + Blur, + /// Casts the content's silhouette as a soft shadow beneath it. + DropShadow, + /// Casts a soft shadow inward from the content's edges, as if it were cut out of the surface. + InnerShadow, + /// Surrounds the content with a soft glow of its silhouette. + OuterGlow, +} + +impl std::fmt::Display for EffectKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Self::Blur => "Blur", + Self::DropShadow => "Drop Shadow", + Self::InnerShadow => "Inner Shadow", + Self::OuterGlow => "Outer Glow", + }) + } +} + +/// One image operation of an [`Effects`] list: an [`EffectKind`] plus its parameters, carried as attributes on the inner item. +/// Each parameter's attribute is named after the input of the node that stamps the kind. Attributes the kind doesn't take are ignored. +#[derive(Clone, Debug, Default, PartialEq, CacheHash)] +pub struct Effect(pub Item); + +impl Effect { + /// Creates an effect row carrying out the given operation, stamping only the parameters that differ from their implicit defaults. + pub fn new(operation: &EffectOperation) -> Self { + let mut item = Item::new_from_element(operation.kind()); + operation.clone().visit_parameters(&mut WriteParameters(&mut item)); + Self(item) + } + + /// This effect's kind. + pub fn kind(&self) -> EffectKind { + *self.0.element() + } + + /// Decodes the parameters into an [`EffectOperation`], falling back to the default for any absent attribute. + pub fn operation(&self) -> EffectOperation { + let mut operation = EffectOperation::placeholder(self.kind()); + operation.visit_parameters(&mut ReadParameters(&self.0)); + operation + } +} + +/// An item's ordered list of effects. A blur acts on the result of those before it, while shadows and glows are cast from the content and stack in order beneath it (or within it, for an inner shadow). +#[derive(Clone, Debug, Default, PartialEq, CacheHash)] +pub struct Effects(pub List); + +impl Effects { + /// Creates the list of rows carrying out the given operations in order. + pub fn from_operations(operations: impl IntoIterator) -> Self { + let mut effects = Self::default(); + for operation in operations { + effects.0.push(Item::new_from_element(Effect::new(&operation))); + } + effects + } + + /// The effects in the order they're carried out. + pub fn effects(&self) -> impl Iterator { + self.0.iter_element_values() + } + + /// The effects decoded into the operations renderers carry out, in order. + pub fn operations(&self) -> impl Iterator + '_ { + self.effects().map(Effect::operation) + } + + /// How far past the content the effects together can paint, in document pixels. + pub fn reach(&self) -> f64 { + self.operations().map(|operation| operation.reach()).sum() + } + + pub fn len(&self) -> usize { + self.0.len() + } + + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +/// Appends an effect to the item's list, after those already applied to it. +pub fn stamp_effect(item: &mut Item, effect: Effect) { + item.attribute_mut_or_insert_default::(ATTR_EFFECTS).0.push(Item::new_from_element(effect)); +} + +/// An effect's parameters decoded from its row, the form that renderers carry it out from. Lengths are in document pixels. +#[derive(Clone, Debug, PartialEq)] +pub enum EffectOperation { + Blur { + /// The standard deviation of the Gaussian, or the half width of the box. + radius: f64, + /// Whether to average a box of pixels rather than weighting them by a Gaussian. + box_blur: bool, + /// Whether to blend the colors with their gamma encoding rather than in linear light. + gamma: bool, + }, + DropShadow { + color: Color, + offset: DVec2, + blur: f64, + spread: f64, + }, + InnerShadow { + color: Color, + offset: DVec2, + blur: f64, + spread: f64, + }, + OuterGlow { + color: Color, + blur: f64, + spread: f64, + }, +} + +impl EffectOperation { + /// The operation an effect of this kind carries out when its row gives none of its parameters, matching the defaults of the node that stamps it. + pub fn default_for(kind: EffectKind) -> Self { + Effect(Item::new_from_element(kind)).operation() + } + + pub fn kind(&self) -> EffectKind { + match self { + Self::Blur { .. } => EffectKind::Blur, + Self::DropShadow { .. } => EffectKind::DropShadow, + Self::InnerShadow { .. } => EffectKind::InnerShadow, + Self::OuterGlow { .. } => EffectKind::OuterGlow, + } + } + + /// How far past the content this operation can paint, or reads from, in document pixels. + pub fn reach(&self) -> f64 { + // A Gaussian is negligible past three standard deviations + match self { + Self::Blur { radius, .. } => 3. * radius.max(0.), + Self::DropShadow { offset, blur, spread, .. } => offset.length() + spread.max(0.) + 3. * blur.max(0.), + Self::InnerShadow { offset, blur, .. } => offset.length() + 3. * blur.max(0.), + Self::OuterGlow { blur, spread, .. } => spread.max(0.) + 3. * blur.max(0.), + } + } + + /// An operation of this kind with arbitrary parameters, for a visitor to fill in. + fn placeholder(kind: EffectKind) -> Self { + match kind { + EffectKind::Blur => Self::Blur { + radius: 0., + box_blur: false, + gamma: false, + }, + EffectKind::DropShadow => Self::DropShadow { + color: Color::TRANSPARENT, + offset: DVec2::ZERO, + blur: 0., + spread: 0., + }, + EffectKind::InnerShadow => Self::InnerShadow { + color: Color::TRANSPARENT, + offset: DVec2::ZERO, + blur: 0., + spread: 0., + }, + EffectKind::OuterGlow => Self::OuterGlow { + color: Color::TRANSPARENT, + blur: 0., + spread: 0., + }, + } + } + + /// Visits each parameter with the attribute it rides and its implicit default, the one listing that writing and reading rows share. + fn visit_parameters(&mut self, visitor: &mut impl ParameterVisitor) { + match self { + Self::Blur { radius, box_blur, gamma } => { + visitor.parameter("radius", radius, 0.); + visitor.parameter("box_blur", box_blur, false); + visitor.parameter("gamma", gamma, false); + } + Self::DropShadow { color, offset, blur, spread } | Self::InnerShadow { color, offset, blur, spread } => { + visitor.parameter("color", color, Color::from(SRGBA8::new(0, 0, 0, 0x80))); + visitor.parameter("offset", offset, DVec2::splat(4.)); + visitor.parameter("blur", blur, 4.); + visitor.parameter("spread", spread, 0.); + } + Self::OuterGlow { color, blur, spread } => { + visitor.parameter("color", color, Color::from(SRGBA8::new(0xff, 0xff, 0xbe, 0xbf))); + visitor.parameter("blur", blur, 8.); + visitor.parameter("spread", spread, 0.); + } + } + } +} + +/// What a value needs to ride an attribute. +pub(crate) trait ParameterValue: Clone + Send + Sync + Default + Debug + PartialEq + CacheHash + 'static {} +impl ParameterValue for T {} + +/// Handles each parameter of an operation, given the attribute key it rides and its implicit default. +pub(crate) trait ParameterVisitor { + fn parameter(&mut self, key: &str, value: &mut T, default: T); +} + +/// Stamps each parameter that differs from its default onto a row. +struct WriteParameters<'a>(&'a mut Item); + +impl ParameterVisitor for WriteParameters<'_> { + fn parameter(&mut self, key: &str, value: &mut T, default: T) { + if *value != default { + self.0.set_attribute(key, value.clone()); + } + } +} + +/// Reads each parameter from a row, or its default where the row lacks it. +struct ReadParameters<'a>(&'a Item); + +impl ParameterVisitor for ReadParameters<'_> { + fn parameter(&mut self, key: &str, value: &mut T, default: T) { + *value = self.0.attribute::(key).cloned().unwrap_or(default); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rows_round_trip_their_operations_eliding_defaults() { + let operations = [ + EffectOperation::Blur { + radius: 6., + box_blur: true, + gamma: false, + }, + EffectOperation::default_for(EffectKind::DropShadow), + EffectOperation::InnerShadow { + color: Color::RED, + offset: DVec2::new(-2., 3.), + blur: 1., + spread: 5., + }, + EffectOperation::OuterGlow { + color: Color::BLUE, + blur: 8., + spread: 2., + }, + ]; + + for operation in operations { + assert_eq!(Effect::new(&operation).operation(), operation); + } + + let default_row = Effect::new(&EffectOperation::default_for(EffectKind::DropShadow)); + assert_eq!(default_row.0.attributes().iter_any().count(), 0, "a default operation stamps no parameters"); + } +} diff --git a/node-graph/libraries/graphic-types/src/lib.rs b/node-graph/libraries/graphic-types/src/lib.rs index 78534f8f430..f6f146995b4 100644 --- a/node-graph/libraries/graphic-types/src/lib.rs +++ b/node-graph/libraries/graphic-types/src/lib.rs @@ -1,6 +1,7 @@ pub mod adjustment; pub mod appearance; pub mod artboard; +pub mod effects; pub mod generator; pub mod graphic; @@ -13,6 +14,7 @@ pub use vector_types; pub use adjustment::Adjustment; pub use appearance::{Appearance, Cover, CoverPlacement, Coverage, FillAndStroke, stamp_coverage}; pub use artboard::Artboard; +pub use effects::{Effect, EffectKind, EffectOperation, Effects, stamp_effect}; pub use graphic::{Graphic, IntoGraphicList, TryFromGraphic, Vector}; pub mod migrations { diff --git a/node-graph/libraries/raster-kernels/src/effect_kernels.rs b/node-graph/libraries/raster-kernels/src/effect_kernels.rs new file mode 100644 index 00000000000..1e5e2fcef15 --- /dev/null +++ b/node-graph/libraries/raster-kernels/src/effect_kernels.rs @@ -0,0 +1,343 @@ +//! The image operations that effects are built from, on the CPU: blurring, growing shapes, and shifting. +//! They mirror the renderer's GPU passes, so a raster's effects read the same in a node that needs its pixels as on the canvas. + +use raster_types::parallel_rows::fill_rows; + +/// Blurs and spreads shorter than this, in pixels, leave no visible trace. +pub const NEGLIGIBLE_LENGTH: f64 = 0.01; + +/// The largest Gaussian standard deviation, in pixels, convolved on the image itself rather than a halved copy. +pub const MAX_DIRECT_GAUSSIAN_SIGMA: f64 = 8.; + +/// The largest box half width, in pixels, convolved on the image itself rather than a halved copy. +pub const MAX_DIRECT_BOX_RADIUS: f64 = 24.; + +/// The farthest texel from the center a kernel weighs, which fits both direct limits. +pub const MAX_KERNEL_REACH: usize = 24; + +/// The most times an image is halved for a blur, by which point it is one pixel across. +const MAX_LEVELS: u32 = 16; + +/// A blur's kernel along one axis, sized in pixels. +#[derive(Clone, Copy, Debug)] +pub enum BlurKernel { + Gaussian { sigma: f64 }, + Box { radius: f64 }, +} + +impl BlurKernel { + /// How many times the image is halved before the kernel runs, so the kernel stays within [`MAX_KERNEL_REACH`]. + pub fn levels(self) -> u32 { + let (length, direct_limit) = match self { + Self::Gaussian { sigma } => (sigma, MAX_DIRECT_GAUSSIAN_SIGMA), + Self::Box { radius } => (radius, MAX_DIRECT_BOX_RADIUS), + }; + + (length / direct_limit).log2().ceil().clamp(0., MAX_LEVELS as f64) as u32 + } + + /// The weights of the texels from the center outward for an image halved `levels` times, normalized so the symmetric kernel sums to 1. + pub fn weights(self, levels: u32) -> Vec { + let level_scale = (1 << levels) as f64; + + let weights: Vec = match self { + Self::Gaussian { sigma } => { + // Halving the image and doubling it back blur it too, so the kernel makes up only the rest of the variance + let pyramid_variance = (4_f64.powi(levels as i32) - 1.) / 3.; + let sigma = (sigma * sigma - pyramid_variance).max(0.).sqrt() / level_scale; + if sigma < NEGLIGIBLE_LENGTH { + return vec![1.]; + } + + let reach = ((3. * sigma).ceil() as usize).min(MAX_KERNEL_REACH); + (0..=reach).map(|i| (-((i * i) as f64) / (2. * sigma * sigma)).exp()).collect() + } + // The texels at the ends weigh only the fraction of them the box covers, so the blur grows smoothly with its radius + Self::Box { radius } => { + let radius = radius / level_scale; + let reach = (radius.ceil() as usize).min(MAX_KERNEL_REACH); + (0..=reach).map(|i| (radius + 1. - i as f64).clamp(0., 1.)).collect() + } + }; + + let total = weights[0] + 2. * weights[1..].iter().sum::(); + weights.into_iter().map(|weight| weight / total).collect() + } +} + +/// A pixel value the operations can weigh, sum, and compare: one coverage channel, or premultiplied color channels. +pub trait Sample: Copy + Default + Send + Sync { + fn scaled(self, factor: f32) -> Self; + fn plus(self, other: Self) -> Self; + fn max(self, other: Self) -> Self; +} + +impl Sample for f32 { + fn scaled(self, factor: f32) -> Self { + self * factor + } + fn plus(self, other: Self) -> Self { + self + other + } + fn max(self, other: Self) -> Self { + f32::max(self, other) + } +} + +impl Sample for [f32; 4] { + fn scaled(self, factor: f32) -> Self { + self.map(|channel| channel * factor) + } + fn plus(self, other: Self) -> Self { + std::array::from_fn(|channel| self[channel] + other[channel]) + } + fn max(self, other: Self) -> Self { + std::array::from_fn(|channel| self[channel].max(other[channel])) + } +} + +/// An image of [`Sample`]s in rows, which the operations read and write. +#[derive(Clone, Debug, PartialEq)] +pub struct Plane { + pub width: usize, + pub height: usize, + pub data: Vec, +} + +impl Plane { + pub fn new(width: usize, height: usize) -> Self { + Self { + width, + height, + data: vec![T::default(); width * height], + } + } + + /// A plane of the same size whose pixels are `map` of this one's. + pub fn map(&self, map: impl Fn(T) -> U + Sync) -> Plane { + let mut mapped = Plane::new(self.width, self.height); + fill_rows(&mut mapped.data, self.width, |y, row| { + let source = &self.data[y * self.width..(y + 1) * self.width]; + row.iter_mut().zip(source).for_each(|(target, &value)| *target = map(value)); + }); + mapped + } + + /// Combines this plane with `other`, of the same size, pixel by pixel. + pub fn zip(&self, other: &Plane, combine: impl Fn(T, U) -> V + Sync) -> Plane { + let mut combined = Plane::new(self.width, self.height); + fill_rows(&mut combined.data, self.width, |y, row| { + let range = y * self.width..(y + 1) * self.width; + let pairs = self.data[range.clone()].iter().zip(&other.data[range]); + row.iter_mut().zip(pairs).for_each(|(target, (&a, &b))| *target = combine(a, b)); + }); + combined + } + + /// The plane with its rows and columns swapped, so a horizontal operation can work vertically. O(width × height). + fn transposed(&self) -> Self { + let mut transposed = Self::new(self.height, self.width); + fill_rows(&mut transposed.data, self.height, |x, row| { + row.iter_mut().enumerate().for_each(|(y, target)| *target = self.data[y * self.width + x]); + }); + transposed + } + + /// Runs `operation` on each row, which it reads in full and writes as a new row of `width` pixels. + fn map_rows(&self, width: usize, operation: impl Fn(&[T], &mut [T]) + Sync) -> Self { + let mut result = Self::new(width, self.height); + fill_rows(&mut result.data, width, |y, row| operation(&self.data[y * self.width..(y + 1) * self.width], row)); + result + } + + /// Runs `along_rows` on the plane's rows, then on its columns by way of one transpose there and back. + fn along_both_axes

(self, horizontal: P, vertical: P, along_rows: impl Fn(Self, P) -> Self) -> Self { + let plane = along_rows(self, horizontal); + along_rows(plane.transposed(), vertical).transposed() + } + + /// Blurs the plane by a kernel along each axis. Kernels too wide to convolve directly run on a copy halved along their axis enough times + /// for them to fit, then doubled back. O(width × height) per pass, with a pass for each halving and doubling. + pub fn blurred(self, horizontal: BlurKernel, vertical: BlurKernel) -> Self { + self.along_both_axes(horizontal, vertical, |mut plane, kernel| { + let levels = kernel.levels(); + let length = plane.width; + let level_length = |level: u32| length.div_ceil(1 << level); + + for level in 1..=levels { + plane = plane.map_rows(level_length(level), halve_row); + } + + let weights = kernel.weights(levels); + plane = plane.map_rows(level_length(levels), |source, target| convolve_row(source, target, &weights)); + + for level in (0..levels).rev() { + plane = plane.map_rows(level_length(level), double_row); + } + plane + }) + } + + /// Grows the shapes by a distance in pixels along each axis, in a square like SVG's `feMorphology`. + pub fn dilated(self, horizontal: f64, vertical: f64) -> Self { + self.along_both_axes(horizontal, vertical, |mut plane, distance| { + let length = plane.width; + + // Steps that double, then the remainder, so each step grows the shapes by up to as much as all the steps before it + let (mut covered, mut step) = (0., 1.); + while covered + step <= distance { + plane = plane.map_rows(length, |source, target| dilate_row(source, target, step)); + covered += step; + step *= 2.; + } + if distance - covered >= NEGLIGIBLE_LENGTH { + plane = plane.map_rows(length, |source, target| dilate_row(source, target, distance - covered)); + } + plane + }) + } + + /// Moves the image by a fractional offset in pixels, interpolating between pixels and leaving transparency where it uncovers the edges. + pub fn shifted(self, offset_x: f64, offset_y: f64) -> Self { + self.along_both_axes(offset_x, offset_y, |plane, offset| plane.map_rows(plane.width, |source, target| shift_row(source, target, offset))) + } +} + +/// The row's value at a position measured in pixels from the first pixel's center, interpolating linearly and holding the end pixels beyond the ends. +fn sample_row(row: &[T], position: f64) -> T { + let last = row.len() as f64 - 1.; + let position = position.clamp(0., last.max(0.)); + let index = position.floor() as usize; + let fraction = (position - index as f64) as f32; + + match row.get(index + 1) { + Some(&next) if fraction > 0. => row[index].scaled(1. - fraction).plus(next.scaled(fraction)), + _ => row[index], + } +} + +/// Halves the row by averaging each pair of pixels, the last pixel of an odd row pairing with itself. +fn halve_row(source: &[T], target: &mut [T]) { + for (index, value) in target.iter_mut().enumerate() { + let first = source[2 * index]; + let second = source.get(2 * index + 1).copied().unwrap_or(first); + *value = first.plus(second).scaled(0.5); + } +} + +/// Doubles a halved row back to its length, interpolating at each pixel's center within the pair it was averaged into. +fn double_row(source: &[T], target: &mut [T]) { + for (index, value) in target.iter_mut().enumerate() { + *value = sample_row(source, index as f64 / 2. - 0.25); + } +} + +/// Weighs each pixel's neighbors by the symmetric kernel `weights`, from the center outward, holding the end pixels beyond the ends. +fn convolve_row(source: &[T], target: &mut [T], weights: &[f64]) { + let last = source.len() as isize - 1; + let at = |index: isize| source[index.clamp(0, last) as usize]; + + for (index, value) in target.iter_mut().enumerate() { + let index = index as isize; + let mut sum = at(index).scaled(weights[0] as f32); + for (distance, &weight) in weights.iter().enumerate().skip(1) { + let pair = at(index - distance as isize).plus(at(index + distance as isize)); + sum = sum.plus(pair.scaled(weight as f32)); + } + *value = sum; + } +} + +/// Takes each pixel's maximum with the row's values `step` pixels to either side. +fn dilate_row(source: &[T], target: &mut [T], step: f64) { + for (index, value) in target.iter_mut().enumerate() { + let position = index as f64; + *value = source[index].max(sample_row(source, position - step)).max(sample_row(source, position + step)); + } +} + +/// Moves the row by `offset` pixels, with nothing beyond its ends. +fn shift_row(source: &[T], target: &mut [T], offset: f64) { + let length = source.len() as f64; + for (index, value) in target.iter_mut().enumerate() { + let position = index as f64 - offset; + let floor = position.floor(); + let fraction = (position - floor) as f32; + + let at = |index: f64| if index >= 0. && index < length { source[index as usize] } else { T::default() }; + *value = at(floor).scaled(1. - fraction).plus(at(floor + 1.).scaled(fraction)); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn kernel_weights_sum_to_one() { + for kernel in [ + BlurKernel::Gaussian { sigma: 3. }, + BlurKernel::Gaussian { sigma: 40. }, + BlurKernel::Box { radius: 5.5 }, + BlurKernel::Box { radius: 100. }, + ] { + let levels = kernel.levels(); + let weights = kernel.weights(levels); + let total = weights[0] + 2. * weights[1..].iter().sum::(); + + assert!((total - 1.).abs() < 1e-9, "{kernel:?} weights sum to {total}"); + assert!(weights.len() <= MAX_KERNEL_REACH + 1, "{kernel:?} reaches {} texels", weights.len()); + } + } + + #[test] + fn small_kernels_run_without_halving() { + assert_eq!(BlurKernel::Gaussian { sigma: MAX_DIRECT_GAUSSIAN_SIGMA }.levels(), 0); + assert_eq!( + BlurKernel::Gaussian { + sigma: MAX_DIRECT_GAUSSIAN_SIGMA * 1.01 + } + .levels(), + 1 + ); + assert_eq!(BlurKernel::Box { radius: 1. }.levels(), 0); + } + + #[test] + fn gaussian_blur_spreads_a_point_with_the_requested_deviation() { + // A single bright pixel blurs into the Gaussian itself, whose variance the pyramid must preserve at every level count + for sigma in [2., 20.] { + let size = 401; + let mut plane = Plane::::new(size, 1); + plane.data[size / 2] = 1.; + + let blurred = plane.blurred(BlurKernel::Gaussian { sigma }, BlurKernel::Gaussian { sigma: 0. }); + let total: f32 = blurred.data.iter().sum(); + let variance: f64 = blurred.data.iter().enumerate().map(|(x, &value)| value as f64 * (x as f64 - (size / 2) as f64).powi(2)).sum::() / total as f64; + + assert!((total - 1.).abs() < 1e-3, "sigma {sigma} keeps the total at {total}"); + assert!((variance.sqrt() - sigma).abs() / sigma < 0.05, "sigma {sigma} spreads to a deviation of {}", variance.sqrt()); + } + } + + #[test] + fn dilation_grows_shapes_by_the_distance() { + let mut plane = Plane::::new(21, 21); + plane.data[10 * 21 + 10] = 1.; + + let dilated = plane.dilated(3., 3.); + let covered = dilated.data.iter().filter(|&&value| value >= 0.999).count(); + + assert_eq!(covered, 7 * 7, "a point grows into a 7 by 7 square"); + } + + #[test] + fn shifting_moves_by_fractions_of_a_pixel() { + let mut plane = Plane::::new(4, 1); + plane.data[1] = 1.; + + let shifted = plane.shifted(0.25, 0.); + + assert_eq!(shifted.data, vec![0., 0.75, 0.25, 0.]); + } +} diff --git a/node-graph/libraries/raster-kernels/src/lib.rs b/node-graph/libraries/raster-kernels/src/lib.rs index 650b0b09e63..57217ed2757 100644 --- a/node-graph/libraries/raster-kernels/src/lib.rs +++ b/node-graph/libraries/raster-kernels/src/lib.rs @@ -3,7 +3,10 @@ pub mod adjustments; mod blending; pub mod color_lookup_table; +pub mod effect_kernels; pub mod generators; mod kernel; +pub mod raster_effects; pub use kernel::*; +pub use raster_effects::evaluate_raster_effects; diff --git a/node-graph/libraries/raster-kernels/src/raster_effects.rs b/node-graph/libraries/raster-kernels/src/raster_effects.rs new file mode 100644 index 00000000000..032d54ed5ee --- /dev/null +++ b/node-graph/libraries/raster-kernels/src/raster_effects.rs @@ -0,0 +1,202 @@ +//! Carries out a raster's effects on the CPU at the raster's own resolution, for the nodes that read its pixels. +//! Each step mirrors the renderer's GPU passes, working on premultiplied gamma-encoded channels as they do. + +use crate::effect_kernels::{BlurKernel, NEGLIGIBLE_LENGTH, Plane}; +use core_types::color::{linear_to_srgb, srgb_to_linear}; +use core_types::list::{ATTR_EFFECTS, Item}; +use core_types::{ATTR_OPACITY_FILL, ATTR_TRANSFORM, Color}; +use glam::{DAffine2, DMat2, DVec2}; +use graphic_types::effects::{EffectOperation, Effects}; +use raster_types::{CPU, Image, Raster}; + +/// The most pixels an image may grow to with the margin its effects cast into, beyond which the margin is cut short. +const MAX_PIXELS_WITH_MARGIN: f64 = (1 << 24) as f64; + +/// The raster with its effects carried out into its pixels, grown to hold what they cast past its edges, and with the effects and the fill opacity they used removed. +/// A raster without the effects attribute is returned as it is. +pub fn evaluate_raster_effects(item: Item>) -> Item> { + let Some(effects) = item.attribute::(ATTR_EFFECTS).cloned() else { return item }; + let transform: DAffine2 = item.attribute_cloned_or_default(ATTR_TRANSFORM); + let fill = item.attribute_cloned_or::(ATTR_OPACITY_FILL, 1.) as f32; + + let (raster, attributes) = item.into_parts(); + let (raster, transform) = match evaluate(&raster, transform, &effects, fill) { + Some((image, grown_transform)) => (Raster::new_cpu(image), grown_transform), + None => (raster, transform), + }; + + // Which attributes come out depends only on whether the effects attribute came in, not on what it holds + let mut item = Item::from_parts(raster, attributes); + item.remove_attribute::(ATTR_EFFECTS); + item.remove_attribute::(ATTR_OPACITY_FILL); + item.set_attribute(ATTR_TRANSFORM, transform); + item +} + +/// The image of the effects carried out on `raster`, which `transform` places in the document, with the transform placing that larger image. +/// `None` when there's nothing to carry out, or the image is empty or its transform can't be inverted. +fn evaluate(raster: &Raster, transform: DAffine2, effects: &Effects, fill: f32) -> Option<(Image, DAffine2)> { + let (width, height) = (raster.width as usize, raster.height as usize); + if (effects.is_empty() && fill >= 1.) || width == 0 || height == 0 || transform.matrix2.determinant() == 0. { + return None; + } + let size = DVec2::new(width as f64, height as f64); + + // Document lengths become pixels along each of the image's axes, and offsets map through the inverse of its pixel placement + let pixels_to_document = transform.matrix2 * DMat2::from_diagonal(size.recip()); + let document_to_pixels = pixels_to_document.inverse(); + let pixels_per_length = DVec2::new(pixels_to_document.x_axis.length(), pixels_to_document.y_axis.length()).recip(); + + // A margin around the image to hold the blur and shadows cast past its edges, cut short for an image that would grow too large + let mut margin = (effects.reach() * pixels_per_length).ceil(); + let grown_size = size + 2. * margin; + if grown_size.x * grown_size.y > MAX_PIXELS_WITH_MARGIN { + let shrunk_size = grown_size * (MAX_PIXELS_WITH_MARGIN / (grown_size.x * grown_size.y)).sqrt(); + margin = ((shrunk_size - size) / 2.).max(DVec2::ZERO).floor(); + } + let (margin_x, margin_y) = (margin.x as usize, margin.y as usize); + + // The content at full strength, which casts every silhouette, and the content faded by the fill, which shows in the result + let mut full = Plane::<[f32; 4]>::new(width + 2 * margin_x, height + 2 * margin_y); + for (y, row) in raster.data.chunks_exact(width).enumerate() { + let start = (y + margin_y) * full.width + margin_x; + full.data[start..start + width] + .iter_mut() + .zip(row) + .for_each(|(target, &color)| *target = premultiplied_gamma_channels(color)); + } + let mut filled = (fill < 1.).then(|| full.map(|pixel| pixel.map(|channel| channel * fill))); + // The shadows and glows stacked beneath the content + let mut beneath: Option> = None; + + for effect in effects.operations() { + match effect { + EffectOperation::Blur { radius, box_blur, gamma } => { + let radius = radius * pixels_per_length; + if radius.max_element() < NEGLIGIBLE_LENGTH { + continue; + } + + // What lies beneath blurs together with the content + if let Some(beneath) = beneath.take() { + filled = filled.map(|filled| composite_beneath(&filled, &beneath, &full)); + full = composite_beneath(&full, &beneath, &full); + } + + let kernels = match box_blur { + true => (BlurKernel::Box { radius: radius.x }, BlurKernel::Box { radius: radius.y }), + false => (BlurKernel::Gaussian { sigma: radius.x }, BlurKernel::Gaussian { sigma: radius.y }), + }; + full = blur(full, kernels, !gamma); + filled = filled.map(|filled| blur(filled, kernels, !gamma)); + } + EffectOperation::DropShadow { color, offset, blur, spread } => { + let coverage = cast(&full, document_to_pixels * offset, blur * pixels_per_length, spread * pixels_per_length, false); + beneath = Some(accumulate(beneath, &coverage, color)); + } + EffectOperation::OuterGlow { color, blur, spread } => { + let coverage = cast(&full, DVec2::ZERO, blur * pixels_per_length, spread * pixels_per_length, false); + beneath = Some(accumulate(beneath, &coverage, color)); + } + EffectOperation::InnerShadow { color, offset, blur, spread } => { + let coverage = cast(&full, document_to_pixels * offset, blur * pixels_per_length, spread * pixels_per_length, true); + filled = filled.map(|filled| composite_within(&filled, &coverage, &full, color)); + full = composite_within(&full, &coverage, &full, color); + } + } + } + + let result = match (filled, beneath) { + (filled, Some(beneath)) => composite_beneath(filled.as_ref().unwrap_or(&full), &beneath, &full), + (Some(filled), None) => filled, + (None, None) => full, + }; + let image = Image { + width: result.width as u32, + height: result.height as u32, + data: result.data.into_iter().map(straight_linear_color).collect(), + }; + + // The grown image spans the margin on each side of the original's unit square + let unit_margin = DVec2::new(margin_x as f64, margin_y as f64) / size; + let grown_transform = transform * DAffine2::from_translation(-unit_margin) * DAffine2::from_scale(DVec2::ONE + 2. * unit_margin); + + Some((image, grown_transform)) +} + +/// A color as the premultiplied, gamma-encoded channels the effects work with. +fn premultiplied_gamma_channels(color: Color) -> [f32; 4] { + let [red, green, blue, alpha] = color.to_gamma_srgb_channels(); + [red * alpha, green * alpha, blue * alpha, alpha] +} + +/// Premultiplied, gamma-encoded channels back as a color. +fn straight_linear_color([red, green, blue, alpha]: [f32; 4]) -> Color { + if alpha <= 0. { + return Color::TRANSPARENT; + } + Color::from_gamma_srgb_channels(red / alpha, green / alpha, blue / alpha, alpha.min(1.)) +} + +/// Premultiplied channels with `transfer` applied to their straight color. +fn transferred(transfer: fn(f32) -> f32) -> impl Fn([f32; 4]) -> [f32; 4] + Sync { + move |[red, green, blue, alpha]| { + if alpha <= 0. { + return [0.; 4]; + } + let channel = |value: f32| transfer((value / alpha).max(0.)) * alpha; + [channel(red), channel(green), channel(blue), alpha] + } +} + +/// Blurs premultiplied gamma-encoded pixels by a kernel along each axis, in linear light if `linear`. +fn blur(plane: Plane<[f32; 4]>, (horizontal, vertical): (BlurKernel, BlurKernel), linear: bool) -> Plane<[f32; 4]> { + match linear { + true => plane.map(transferred(srgb_to_linear)).blurred(horizontal, vertical).map(transferred(linear_to_srgb)), + false => plane.blurred(horizontal, vertical), + } +} + +/// The coverage of the content's silhouette (or of the area outside it, if `inverse`), cast by `offset`, grown by `spread` and softened by a Gaussian of deviation `blur`, all in pixels along each axis. +fn cast(content: &Plane<[f32; 4]>, offset: DVec2, blur: DVec2, spread: DVec2, inverse: bool) -> Plane { + let mut coverage = content.map(|[_, _, _, alpha]| alpha).shifted(offset.x, offset.y); + if inverse { + coverage = coverage.map(|alpha| 1. - alpha); + } + + if spread.max_element() >= NEGLIGIBLE_LENGTH { + coverage = coverage.dilated(spread.x, spread.y); + } + if blur.max_element() >= NEGLIGIBLE_LENGTH { + coverage = coverage.blurred(BlurKernel::Gaussian { sigma: blur.x }, BlurKernel::Gaussian { sigma: blur.y }); + } + coverage +} + +/// Stacks a silhouette's coverage, tinted `color`, over what lies `beneath` the content so far. +fn accumulate(beneath: Option>, coverage: &Plane, color: Color) -> Plane<[f32; 4]> { + let color = premultiplied_gamma_channels(color); + let tinted = coverage.map(|coverage| color.map(|channel| channel * coverage)); + + match beneath { + Some(beneath) => tinted.zip(&beneath, |tinted, beneath| std::array::from_fn(|channel| tinted[channel] + beneath[channel] * (1. - tinted[3]))), + None => tinted, + } +} + +/// The image with a silhouette's coverage, tinted `color`, placed over it within the full-strength content. +fn composite_within(image: &Plane<[f32; 4]>, coverage: &Plane, full_content: &Plane<[f32; 4]>, color: Color) -> Plane<[f32; 4]> { + let color = premultiplied_gamma_channels(color); + let coverages = coverage.zip(full_content, |coverage, content| [coverage, content[3], 0., 0.]); + + image.zip(&coverages, |image, [coverage, content_alpha, _, _]| { + let tinted = color.map(|channel| channel * coverage); + std::array::from_fn(|channel| tinted[channel] * content_alpha + image[channel] * (1. - tinted[3])) + }) +} + +/// The image with the shadows and glows `beneath` placed under it, knocked out by the full-strength content. +fn composite_beneath(image: &Plane<[f32; 4]>, beneath: &Plane<[f32; 4]>, full_content: &Plane<[f32; 4]>) -> Plane<[f32; 4]> { + let knocked_out = beneath.zip(full_content, |beneath, content| beneath.map(|channel| channel * (1. - content[3]))); + image.zip(&knocked_out, |image, beneath| std::array::from_fn(|channel| image[channel] + beneath[channel])) +} diff --git a/node-graph/libraries/rendering/Cargo.toml b/node-graph/libraries/rendering/Cargo.toml index 8b6fe208564..bae0b7c77da 100644 --- a/node-graph/libraries/rendering/Cargo.toml +++ b/node-graph/libraries/rendering/Cargo.toml @@ -37,6 +37,7 @@ kurbo = { workspace = true } vector-types = { workspace = true } graphic-types = { workspace = true } vello_gpu = { workspace = true } +wgpu = { workspace = true } parley = { workspace = true } skrifa = { workspace = true } diff --git a/node-graph/libraries/rendering/src/renderer.rs b/node-graph/libraries/rendering/src/renderer.rs index 42ee13ef55c..c224aa871cf 100644 --- a/node-graph/libraries/rendering/src/renderer.rs +++ b/node-graph/libraries/rendering/src/renderer.rs @@ -16,9 +16,9 @@ use core_types::math::quad::Quad; use core_types::transform::Footprint; use core_types::uuid::{NodeId, generate_uuid}; use core_types::{ - ATTR_BACKGROUND, ATTR_BLEND_MODE, ATTR_CLIP, ATTR_CLIPPING_MASK, ATTR_DIMENSIONS, ATTR_EDITOR_CLICK_TARGET, ATTR_EDITOR_LAYER_PATH, ATTR_EDITOR_MERGED_LAYERS, ATTR_EDITOR_TEXT_FRAME, ATTR_FONT, - ATTR_FONT_SIZE, ATTR_GRADIENT_FORM, ATTR_LETTER_SPACING, ATTR_LETTER_TILT, ATTR_LINE_HEIGHT, ATTR_LOCATION, ATTR_MAX_HEIGHT, ATTR_MAX_WIDTH, ATTR_OPACITY, ATTR_OPACITY_FILL, ATTR_TEXT_ALIGN, - ATTR_TRANSFORM, + ATTR_BACKGROUND, ATTR_BLEND_MODE, ATTR_CLIP, ATTR_CLIPPING_MASK, ATTR_DIMENSIONS, ATTR_EDITOR_CLICK_TARGET, ATTR_EDITOR_LAYER_PATH, ATTR_EDITOR_MERGED_LAYERS, ATTR_EDITOR_TEXT_FRAME, + ATTR_EFFECTS, ATTR_FONT, ATTR_FONT_SIZE, ATTR_GRADIENT_FORM, ATTR_LETTER_SPACING, ATTR_LETTER_TILT, ATTR_LINE_HEIGHT, ATTR_LOCATION, ATTR_MAX_HEIGHT, ATTR_MAX_WIDTH, ATTR_OPACITY, + ATTR_OPACITY_FILL, ATTR_TEXT_ALIGN, ATTR_TRANSFORM, }; use dyn_any::DynAny; use glam::{DAffine2, DMat2, DVec2}; @@ -31,7 +31,7 @@ use graphic_types::vector_types::vector::style::{RenderMode, StrokeAlign, Stroke use graphic_types::vector_types::vector::{ GeometryCache, Path, PlacedContours, contour_bezpaths, contours_bounding_box, contours_stroke_inclusive_bounding_box, edit_item_geometry, edit_list_row_geometry, no_paths, }; -use graphic_types::{Appearance, Artboard, Cover, Coverage, FillAndStroke, Graphic, Vector}; +use graphic_types::{Appearance, Artboard, Cover, Coverage, Effects, FillAndStroke, Graphic, Vector}; use kurbo::{Affine, BezPath, Cap, Join, PathEl, Shape}; use num_traits::Zero; use skrifa::instance::{LocationRef, NormalizedCoord, Size}; @@ -96,6 +96,32 @@ impl<'a, T> ItemRef<'a, T> { (self.attribute_cloned_or::(ATTR_OPACITY, 1.) * opacity_fill) as f32 } + /// How the item's opacity attributes and effects apply as it renders. + fn compositing(self, render_params: &RenderParams) -> ItemCompositing<'a> { + let opacity = self.attribute_cloned_or::(ATTR_OPACITY, 1.) as f32; + let fill = if render_params.for_mask { + 1. + } else { + self.attribute_cloned_or::(ATTR_OPACITY_FILL, 1.) as f32 + }; + + // Outline mode shows only geometry + let effects = self + .attribute::(ATTR_EFFECTS) + .filter(|effects| !effects.is_empty() && render_params.render_mode != RenderMode::Outline); + + match effects { + Some(effects) => ItemCompositing { + opacity, + effects: Some((effects, fill)), + }, + None => ItemCompositing { + opacity: opacity * fill, + effects: None, + }, + } + } + pub(crate) fn clone_item_attributes(self) -> core_types::list::ItemAttributeValues { match self { ItemRef::ListItem(list, index) => list.clone_item_attributes(index), @@ -161,6 +187,260 @@ impl<'a> ItemRef<'a, Vector> { } } +/// How an item composites: the alpha of the layer it draws through, and the effects its content renders through inside that layer. +struct ItemCompositing<'a> { + opacity: f32, + /// With the fill opacity, which the effects group applies to the content alone so the shadows and glows cast from it stay at full strength. + effects: Option<(&'a Effects, f32)>, +} + +impl ItemCompositing<'_> { + /// How far the effects reach past the content, in document pixels. + fn reach(&self) -> f64 { + self.effects.map_or(0., |(effects, _)| effects.reach()) + } + + /// Draws the content `draw` draws through the effects group, given the content's bounds in the scene's space. + /// Rendering a split scene calls `draw` again for each part. + fn draw_with_effects(&self, scene: &mut VelloSceneBuilder, bounds: impl FnOnce() -> RenderBoundingBox, mut draw: impl FnMut(&mut VelloSceneBuilder)) { + let Some((effects, fill)) = self.effects else { return draw(scene) }; + + let bounds = match bounds() { + RenderBoundingBox::Rectangle([min, max]) => Some(kurbo::Rect::new(min.x, min.y, max.x, max.y)), + RenderBoundingBox::Infinite => None, + RenderBoundingBox::None => return draw(scene), + }; + scene.draw_effects(effects, fill, bounds, &mut draw); + } + + /// The opacity for content drawn without the effects group, as SVG draws content its filter can't apply to. + fn opacity_without_effects(&self) -> f32 { + self.opacity * self.effects.map_or(1., |(_, fill)| fill) + } + + /// Writes the SVG filter carrying out the effects, if there are any to apply, given the content's bounds in the current user space. + /// Returns the reference for the `filter` attribute of a group wrapping the content, which also takes the item's opacity and blend mode. + fn svg_filter(&self, render: &mut SvgRender, bounds: impl FnOnce() -> RenderBoundingBox) -> Option { + let (effects, fill) = self.effects?; + + // Effects change nothing visible about content without edges, like a color or gradient layer filling everything + let RenderBoundingBox::Rectangle(bounds) = bounds() else { return None }; + + let id = write_svg_effects_filter(&mut render.svg_defs, effects, fill, bounds, render.transform)?; + Some(format!("url(#{id})")) + } +} + +/// How far the effects of the items nested anywhere inside `graphic` reach past its bounds, in document pixels. +fn nested_effects_reach(graphic: &Graphic) -> f64 { + fn item_reach(item: &Item) -> f64 { + item.attribute::(ATTR_EFFECTS).map_or(0., Effects::reach) + } + fn list_reach(list: &List, nested: impl Fn(&T) -> f64) -> f64 { + (0..list.len()) + .map(|index| list.attribute::(ATTR_EFFECTS, index).map_or(0., Effects::reach) + list.element(index).map_or(0., &nested)) + .fold(0., f64::max) + } + + match graphic { + Graphic::None(_) | Graphic::NoneList(_) | Graphic::StrokeList(_) => 0., + Graphic::Graphic(item) => item_reach(item) + nested_effects_reach(item.element()), + Graphic::Vector(item) => item_reach(item), + Graphic::RasterCPU(item) => item_reach(item), + Graphic::RasterGPU(item) => item_reach(item), + Graphic::Color(item) => item_reach(item), + Graphic::Gradient(item) => item_reach(item), + Graphic::Text(item) => item_reach(item), + Graphic::GraphicList(list) => list_reach(list, nested_effects_reach), + Graphic::VectorList(list) => list_reach(list, |_| 0.), + Graphic::RasterCPUList(list) => list_reach(list, |_| 0.), + Graphic::RasterGPUList(list) => list_reach(list, |_| 0.), + Graphic::ColorList(list) => list_reach(list, |_| 0.), + Graphic::GradientList(list) => list_reach(list, |_| 0.), + Graphic::TextList(list) => list_reach(list, |_| 0.), + } +} + +/// Wraps an item's SVG `content` in a group applying its effects `filter`, with the item's opacity and blend mode, if there is a filter. +fn render_svg_through_filter(render: &mut SvgRender, filter: Option, opacity: f32, blend_mode: BlendMode, content: impl FnOnce(&mut SvgRender)) { + let Some(filter) = filter else { return content(render) }; + + render.parent_tag( + "g", + |attributes| { + attributes.push("filter", filter); + if opacity < 1. { + attributes.push("opacity", opacity.to_string()); + } + if blend_mode != BlendMode::default() { + attributes.push("style", blend_mode.render()); + } + }, + content, + ); +} + +/// Builds the primitives of an SVG filter, naming each result so later primitives can take it as an input. +#[derive(Default)] +struct SvgFilterPrimitives { + markup: String, + count: usize, +} + +impl SvgFilterPrimitives { + /// Appends a primitive tag with `attributes` (and `children`, if any), returning the name of its result. + fn push(&mut self, tag: &str, attributes: &str, children: &str) -> String { + self.count += 1; + let result = format!("effect-{}", self.count); + + match children.is_empty() { + true => write!(self.markup, r#"<{tag} {attributes} result="{result}" />"#), + false => write!(self.markup, r#"<{tag} {attributes} result="{result}">{children}"#), + } + .unwrap(); + + result + } + + /// The sum of two premultiplied images, for composites whose terms SVG's other operators can't express. + fn add(&mut self, first: &str, second: &str) -> String { + self.push("feComposite", &format!(r#"in="{first}" in2="{second}" operator="arithmetic" k2="1" k3="1""#), "") + } + + /// The image with the shadows and glows `beneath` it, knocked out by the full-strength content so a faded fill doesn't reveal them. + fn composite_beneath(&mut self, image: &str, beneath: &str, full_content: &str) -> String { + let knocked_out = self.push("feComposite", &format!(r#"in="{beneath}" in2="{full_content}" operator="out""#), ""); + self.add(image, &knocked_out) + } +} + +/// Writes an SVG filter into `svg_defs` that carries out `effects` like the GPU passes do, returning its ID, or `None` if the user space is degenerate. +/// `bounds` is the content's extent in the filtered group's user space, which `user_to_document` maps into the document space the effects' lengths are measured in. +fn write_svg_effects_filter(svg_defs: &mut String, effects: &Effects, fill: f32, bounds: [DVec2; 2], user_to_document: DAffine2) -> Option { + use graphic_types::EffectOperation; + + let determinant = user_to_document.matrix2.determinant(); + if !determinant.is_normal() { + return None; + } + let document_to_user = user_to_document.matrix2.inverse(); + let length_scale = determinant.abs().sqrt().recip(); + + let mut primitives = SvgFilterPrimitives::default(); + + // The content at full strength, which casts every silhouette, and the content faded by the fill, which shows in the result + let mut full = "SourceGraphic".to_string(); + let mut filled = (fill < 1.).then(|| primitives.push("feComponentTransfer", r#"in="SourceGraphic""#, &format!(r#""#))); + // The shadows and glows stacked beneath the content + let mut beneath: Option = None; + + for effect in effects.operations() { + let (color, offset, blur, spread, inner) = match effect { + EffectOperation::Blur { radius, box_blur, gamma } => { + let radius = radius * length_scale; + if radius < 0.01 { + continue; + } + + // What lies beneath blurs together with the content + if let Some(beneath) = beneath.take() { + filled = filled.map(|filled| primitives.composite_beneath(&filled, &beneath, &full)); + full = primitives.composite_beneath(&full, &beneath, &full); + } + + // SVG has no box blur, so a box becomes the Gaussian of the same variance + let sigma = if box_blur { (radius * (radius + 1.) / 3.).sqrt() } else { radius }; + let space = if gamma { "sRGB" } else { "linearRGB" }; + let attributes = |input: &str| format!(r#"in="{input}" stdDeviation="{sigma}" color-interpolation-filters="{space}""#); + + filled = filled.map(|filled| primitives.push("feGaussianBlur", &attributes(&filled), "")); + full = primitives.push("feGaussianBlur", &attributes(&full), ""); + continue; + } + EffectOperation::DropShadow { color, offset, blur, spread } => (color, offset, blur, spread, false), + EffectOperation::OuterGlow { color, blur, spread } => (color, DVec2::ZERO, blur, spread, false), + EffectOperation::InnerShadow { color, offset, blur, spread } => (color, offset, blur, spread, true), + }; + + // The silhouette, or the area outside it for an inner shadow, cast by the offset, grown by the spread, and softened by the blur + let offset = document_to_user * offset; + let mut coverage = match offset == DVec2::ZERO { + true => full.clone(), + false => primitives.push("feOffset", &format!(r#"in="{full}" dx="{}" dy="{}""#, offset.x, offset.y), ""), + }; + if inner { + let opaque = primitives.push("feFlood", r#"flood-opacity="1""#, ""); + coverage = primitives.push("feComposite", &format!(r#"in="{opaque}" in2="{coverage}" operator="out""#), ""); + } + if spread * length_scale >= 0.01 { + coverage = primitives.push("feMorphology", &format!(r#"in="{coverage}" operator="dilate" radius="{}""#, spread * length_scale), ""); + } + if blur * length_scale >= 0.01 { + coverage = primitives.push("feGaussianBlur", &format!(r#"in="{coverage}" stdDeviation="{}""#, blur * length_scale), ""); + } + + let flood = primitives.push( + "feFlood", + &format!(r##"flood-color="#{}" flood-opacity="{}""##, SRGBA8::from(color.with_alpha(1.)).to_rgb_hex(), color.a()), + "", + ); + let tinted = primitives.push("feComposite", &format!(r#"in="{flood}" in2="{coverage}" operator="in""#), ""); + + if inner { + // Placed over the image within the full-strength content, so it stays visible over a faded fill + let within = primitives.push("feComposite", &format!(r#"in="{tinted}" in2="{full}" operator="in""#), ""); + let mut over = |image: &str| { + let uncovered = primitives.push("feComposite", &format!(r#"in="{image}" in2="{tinted}" operator="out""#), ""); + primitives.add(&within, &uncovered) + }; + filled = filled.map(|filled| over(&filled)); + full = over(&full); + } else { + beneath = Some(match beneath { + Some(beneath) => primitives.push("feComposite", &format!(r#"in="{tinted}" in2="{beneath}" operator="over""#), ""), + None => tinted, + }); + } + } + + // The filter's result is its last primitive's, so finish with the image shown + let shown = filled.unwrap_or_else(|| full.clone()); + match beneath { + Some(beneath) => { + primitives.composite_beneath(&shown, &beneath, &full); + } + None => { + primitives.push("feOffset", &format!(r#"in="{shown}""#), ""); + } + } + + // The filter covers how far the effects reach past the content + let reach = effects.reach() * length_scale; + let (min, max) = (bounds[0] - reach, bounds[1] + reach); + let id = format!("effects-{}", generate_uuid()); + write!( + svg_defs, + r#"{}"#, + min.x, + min.y, + max.x - min.x, + max.y - min.y, + primitives.markup + ) + .unwrap(); + + Some(id) +} + +/// Grows a layer's bounds in the scene's space by a length in document pixels. +fn inflate_bounds(bounds: [DVec2; 2], amount: f64) -> kurbo::Rect { + kurbo::Rect::new(bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y).inflate(amount, amount) +} + +fn rect_bounds(rect: kurbo::Rect) -> RenderBoundingBox { + RenderBoundingBox::Rectangle([DVec2::new(rect.x0, rect.y0), DVec2::new(rect.x1, rect.y1)]) +} + /// The color one paint item contributes, faded by its opacity attributes. pub(crate) fn faded_paint_color(item: ItemRef<'_, Color>, for_mask: bool) -> Option { let color = item.element()?; @@ -214,7 +494,7 @@ impl MaskType { pub struct SvgRender { pub svg: Vec, pub svg_defs: String, - /// Maps the user space of the content being rendered into the document's space. + /// Maps the user space of the content being rendered into the document's space, where the lengths of effects are measured. pub transform: DAffine2, /// The images drawn to canvases rather than embedded, by the canvas ID their placeholders name. pub image_data: HashMap, @@ -708,6 +988,8 @@ pub struct RenderMetadata { /// Per-layer `ATTR_APPEARANCE` item attribute, exposed so message handlers can read it. #[cfg_attr(feature = "serde", serde(skip))] pub appearance_attributes: HashMap>, + /// Per-layer distance its effects paint past its content, in document pixels, for layers with effects. + pub effects_reach: HashMap, pub backgrounds: Vec, } @@ -733,6 +1015,7 @@ impl RenderMetadata { clip_targets, vector_data, appearance_attributes, + effects_reach, backgrounds, } = self; upstream_footprints.extend(other.upstream_footprints.iter()); @@ -745,6 +1028,7 @@ impl RenderMetadata { clip_targets.extend(other.clip_targets.iter()); vector_data.extend(other.vector_data.iter().map(|(id, data)| (*id, data.clone()))); appearance_attributes.extend(other.appearance_attributes.iter().map(|(id, data)| (*id, data.clone()))); + effects_reach.extend(other.effects_reach.iter()); // TODO: Find a better non O(n^2) way to merge backgrounds for background in &other.backgrounds { @@ -800,14 +1084,13 @@ fn render_graphic_item_svg(item: ItemRef<'_, Graphic>, next_clips: bool, mask_st let Some(element) = item.element() else { return }; let transform: DAffine2 = item.attribute_cloned_or_default(ATTR_TRANSFORM); let blend_mode: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); - let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.); - let opacity_fill_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY_FILL, 1.); // This item's declared appearance (if any) cascades to descendants lacking their own let child_render_params = item .attribute::(ATTR_APPEARANCE) .and_then(Appearance::declared) .map(|appearance| render_params.for_child_item(appearance)); let render_params = child_render_params.as_ref().unwrap_or(render_params); + let compositing = item.compositing(render_params); let matrix = format_transform_matrix(transform); let mut masked_by = None; @@ -838,29 +1121,47 @@ fn render_graphic_item_svg(item: ItemRef<'_, Graphic>, next_clips: bool, mask_st } let render_item = |render: &mut SvgRender| { - render.parent_tag( - "g", - |attributes| { - if !matrix.is_empty() { - attributes.push(ATTR_TRANSFORM, matrix.clone()); - } + // The filter also covers how far the effects nested inside reach past the content, measured in this user space + let document_scale = render.transform.matrix2.determinant().abs().sqrt(); + let filter = compositing.svg_filter(render, || match element.bounding_box(transform, true) { + RenderBoundingBox::Rectangle([min, max]) => { + let nested_reach = nested_effects_reach(element) / document_scale; + RenderBoundingBox::Rectangle([min - nested_reach, max + nested_reach]) + } + bounds => bounds, + }); + let filtered = filter.is_some(); - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; - if opacity < 1. { - attributes.push("opacity", opacity.to_string()); - } + render_svg_through_filter(render, filter, compositing.opacity, blend_mode, |render| { + render.parent_tag( + "g", + |attributes| { + if !matrix.is_empty() { + attributes.push(ATTR_TRANSFORM, matrix.clone()); + } - if blend_mode != BlendMode::default() { - attributes.push("style", blend_mode.render()); - } - }, - |render| { - let parent_transform = render.transform; - render.transform = parent_transform * transform; - element.render_svg(render, render_params); - render.transform = parent_transform; - }, - ); + // A filter's wrapping group takes the opacity and blend mode instead + if filtered { + return; + } + + let opacity = compositing.opacity_without_effects(); + if opacity < 1. { + attributes.push("opacity", opacity.to_string()); + } + + if blend_mode != BlendMode::default() { + attributes.push("style", blend_mode.render()); + } + }, + |render| { + let parent_transform = render.transform; + render.transform = parent_transform * transform; + element.render_svg(render, render_params); + render.transform = parent_transform; + }, + ); + }); }; // The mask rides an untransformed wrapper so it resolves in this list's space rather than the item's own @@ -885,54 +1186,63 @@ fn render_graphic_item_to_vello<'a>( let item_transform: DAffine2 = item.attribute_cloned_or_default(ATTR_TRANSFORM); let transform = transform * item_transform; let blend_mode_attr: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); - let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.); - let opacity_fill_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY_FILL, 1.); // This item's declared appearance (if any) cascades to descendants lacking their own let child_render_params = item .attribute::(ATTR_APPEARANCE) .and_then(Appearance::declared) .map(|appearance| render_params.for_child_item(appearance)); let render_params = child_render_params.as_ref().unwrap_or(render_params); - - let mut layer = false; + let compositing = item.compositing(render_params); let blend_mode = match render_params.render_mode { RenderMode::Outline => peniko::Mix::Normal, _ => blend_mode_attr.to_peniko(), }; - let mut bounds = RenderBoundingBox::None; + let needs_layer = compositing.opacity < 1. || (render_params.render_mode != RenderMode::Outline && blend_mode_attr != BlendMode::default()); - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; - if opacity < 1. || (render_params.render_mode != RenderMode::Outline && blend_mode_attr != BlendMode::default()) { - bounds = element.bounding_box(transform, true); + // The bounds are only worth finding for the layers or effects group that need them + let bounds = if needs_layer || mask_element_and_transform.is_some() || compositing.effects.is_some() { + element.bounding_box(transform, true) + } else { + RenderBoundingBox::None + }; + let layer_rect = match bounds { + RenderBoundingBox::Rectangle(bounds) => Some(inflate_bounds(bounds, compositing.reach())), + _ => None, + }; - if let RenderBoundingBox::Rectangle(bounds) = bounds { - scene.push_layer( - peniko::Fill::NonZero, - peniko::BlendMode::new(blend_mode, peniko::Compose::SrcOver), - opacity, - kurbo::Affine::IDENTITY, - &kurbo::Rect::new(bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y), - ); - layer = true; - } - } + let layer = if needs_layer && let Some(rect) = layer_rect { + scene.push_layer( + peniko::Fill::NonZero, + peniko::BlendMode::new(blend_mode, peniko::Compose::SrcOver), + compositing.opacity, + kurbo::Affine::IDENTITY, + &rect, + ); + true + } else { + false + }; + + // The effects group also covers how far the effects nested inside reach past the content + let effects_bounds = || match bounds { + RenderBoundingBox::Rectangle(bounds) => rect_bounds(inflate_bounds(bounds, nested_effects_reach(element))), + bounds => bounds, + }; + let draw_content = |scene: &mut VelloSceneBuilder| { + compositing.draw_with_effects(scene, effects_bounds, |scene| element.render_to_vello(scene, transform, render_params)); + }; if next_clips && mask_element_and_transform.is_none() { *mask_element_and_transform = Some((element, transform)); - element.render_to_vello(scene, transform, render_params); + draw_content(scene); } else if let Some((mask_element, transform_mask)) = *mask_element_and_transform { if !next_clips { *mask_element_and_transform = None; } - if !layer { - bounds = element.bounding_box(transform, true); - } - - if let RenderBoundingBox::Rectangle(bounds) = bounds { - let rect = kurbo::Rect::new(bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y); + if let Some(rect) = layer_rect { scene.push_layer(peniko::Fill::NonZero, peniko::Mix::Normal, 1., kurbo::Affine::IDENTITY, &rect); mask_element.render_to_vello(scene, transform_mask, &render_params.for_clipper()); scene.push_layer( @@ -944,14 +1254,14 @@ fn render_graphic_item_to_vello<'a>( ); } - element.render_to_vello(scene, transform, render_params); + draw_content(scene); - if matches!(bounds, RenderBoundingBox::Rectangle(_)) { + if layer_rect.is_some() { scene.pop_layer(); scene.pop_layer(); } } else { - element.render_to_vello(scene, transform, render_params); + draw_content(scene); } if layer { @@ -969,6 +1279,13 @@ fn collect_graphic_item_metadata(item: ItemRef<'_, Graphic>, metadata: &mut Rend let mut footprint = footprint; footprint.transform *= item_transform; + if let Some(layer) = item.layer() { + let reach = item.attribute::(ATTR_EFFECTS).map_or(0., Effects::reach) + nested_effects_reach(element); + if reach > 0. { + metadata.effects_reach.insert(layer, reach); + } + } + // An anonymous wrapper item (no layer tag) still recurses to reach nested content with "editor:layer_path" attributes element.collect_metadata(metadata, footprint, item.layer(), child_appearance); } @@ -1459,12 +1776,12 @@ impl Render for List { } /// Emits one vector shape as SVG, with no wrapping group of its own. -fn render_vector_shape_svg(item: ItemRef<'_, Vector>, vector: &Vector, render: &mut SvgRender, render_params: &RenderParams) { +/// When `filtered`, the group applying the item's effects takes its opacity and blend mode instead. +fn render_vector_shape_svg(item: ItemRef<'_, Vector>, vector: &Vector, filtered: bool, render: &mut SvgRender, render_params: &RenderParams) { let paths = item.attribute::>(ATTR_PATHS).unwrap_or_else(|| no_paths()); let item_transform: DAffine2 = item.attribute_cloned_or_default(ATTR_TRANSFORM); - let blend_mode_attr: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); - let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.); - let opacity_fill_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY_FILL, 1.); + let blend_mode_attr: BlendMode = if filtered { BlendMode::default() } else { item.attribute_cloned_or_default(ATTR_BLEND_MODE) }; + let opacity = if filtered { 1. } else { item.compositing(render_params).opacity_without_effects() }; // The item's own declared appearance wins over one cascading down from an ancestor let own_appearance = item.attribute::(ATTR_APPEARANCE).and_then(Appearance::declared); @@ -1638,7 +1955,6 @@ fn render_vector_shape_svg(item: ItemRef<'_, Vector>, vector: &Vector, render: & attributes.push_val(stroke_shape_attribute); attributes.push_val(stroke_attribute); - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; if opacity < 1. { attributes.push("opacity", opacity.to_string()); } @@ -1671,7 +1987,10 @@ fn render_vector_item_svg(item: ItemRef<'_, Vector>, next_clips: bool, clip_mask let mut masked_by = None; if next_clips && clip_mask_state.is_none() { - let masker = Graphic::VectorList(List::new_from_item(Item::from_parts(vector.clone(), item.clone_item_attributes()))); + // The shadows and glows the masker casts don't widen the area its clipped content shows in + let mut masker = Item::from_parts(vector.clone(), item.clone_item_attributes()); + masker.remove_attribute::(ATTR_EFFECTS); + let masker = Graphic::VectorList(List::new_from_item(masker)); let mask_type = if masker.can_reduce_to_clip_path() { MaskType::Clip } else { MaskType::Mask }; let uuid = generate_uuid(); @@ -1689,22 +2008,39 @@ fn render_vector_item_svg(item: ItemRef<'_, Vector>, next_clips: bool, clip_mask masked_by = Some((mask_type.to_attribute(), format!("url(#mask-{uuid})"))); } + let compositing = item.compositing(render_params); + let render_shape = |render: &mut SvgRender| { + let filter = compositing.svg_filter(render, || { + let item_transform: DAffine2 = item.attribute_cloned_or_default(ATTR_TRANSFORM); + let appearance = item.attribute::(ATTR_APPEARANCE).and_then(Appearance::declared); + let stroke = appearance.or(render_params.inherited_appearance.as_ref()).and_then(|appearance| appearance.fill_and_stroke().stroke); + let paths = item.attribute::>(ATTR_PATHS).unwrap_or_else(|| no_paths()); + + vector + .stroke_inclusive_bounding_box_with_transform(paths, item_transform, stroke.as_ref()) + .map_or(RenderBoundingBox::None, RenderBoundingBox::Rectangle) + }); + let filtered = filter.is_some(); + let blend_mode: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); + + render_svg_through_filter(render, filter, compositing.opacity, blend_mode, |render| { + render_vector_shape_svg(item, vector, filtered, render, render_params) + }); + }; + // Item geometry is baked into the path data instead of a group transform, so mask coordinates line up match masked_by { - Some((attribute, selector)) => render.parent_tag( - "g", - |attributes| attributes.push(attribute, selector), - |render| render_vector_shape_svg(item, vector, render, render_params), - ), - None => render_vector_shape_svg(item, vector, render, render_params), + Some((attribute, selector)) => render.parent_tag("g", |attributes| attributes.push(attribute, selector), render_shape), + None => render_shape(render), } } -/// Whether `bounds` placed by `transform` overlaps the scene once widened by `inflation`, which is measured in the space `inflation_transform` maps from. -fn overlaps_scene(scene: &VelloSceneBuilder, bounds: [DVec2; 2], transform: DAffine2, inflation: f64, inflation_transform: DAffine2) -> bool { +/// Whether `bounds` placed by `transform` overlaps the scene once widened by `inflation`, measured in the space `inflation_transform` maps from, and by its effects' `reach`, measured in the scene's space. +fn overlaps_scene(scene: &VelloSceneBuilder, bounds: [DVec2; 2], transform: DAffine2, inflation: f64, inflation_transform: DAffine2, reach: f64) -> bool { let view = DAffine2::from_cols_array(&scene.view().as_coeffs()); let (largest_scale, _) = singular_values(view * inflation_transform); - let inflation = inflation * largest_scale; + let (largest_view_scale, _) = singular_values(view); + let inflation = inflation * largest_scale + reach * largest_view_scale; let [min, max] = (view * transform * Quad::from_box(bounds)).bounding_box(); min.x - inflation < scene.width() as f64 && max.x + inflation > 0. && min.y - inflation < scene.height() as f64 && max.y + inflation > 0. @@ -1726,8 +2062,7 @@ fn render_vector_item_to_vello( let element_paths = item.attribute::>(ATTR_PATHS).unwrap_or_else(|| no_paths()); let item_transform: DAffine2 = item.attribute_cloned_or_default(ATTR_TRANSFORM); let blend_mode_attr: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); - let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.); - let opacity_fill_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY_FILL, 1.); + let compositing = item.compositing(render_params); let multiplied_transform = parent_transform * item_transform; // The item's own declared appearance wins over one cascading down from an ancestor @@ -1779,7 +2114,7 @@ fn render_vector_item_to_vello( // Vello only culls after expanding strokes and flattening paths, so an item outside the scene is skipped before any of that work let stroke_inflation = stroke.map_or(0., |stroke| stroke.max_aabb_inflation(true)).max(get_outline_styles(render_params).0.width); - if !overlaps_scene(scene, layer_bounds, multiplied_transform, stroke_inflation, element_transform) { + if !overlaps_scene(scene, layer_bounds, multiplied_transform, stroke_inflation, element_transform, compositing.reach()) { if next_clips && clip_masker.is_none() { *clip_masker = Some(List::new_from_item(Item::from_parts(element.clone(), item.clone_item_attributes()))); } else if clip_masker.is_some() && !next_clips { @@ -1791,11 +2126,11 @@ fn render_vector_item_to_vello( let Some(placed) = item.placed_contours(applied_stroke_transform) else { return }; let path = placed.path(); - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; + let opacity = compositing.opacity; let needs_blend_layer = opacity < 1. || blend_mode_attr != BlendMode::default(); - // Shared by the blend and clipping layers below, so it is only worth deriving when one of them is pushed - let layer_geometry = (needs_blend_layer || clip_masker.is_some()).then(|| { + // Shared by the layers and effects group below, so it is only worth deriving when one of them is pushed + let layer_geometry = (needs_blend_layer || clip_masker.is_some() || compositing.effects.is_some()).then(|| { // `max_aabb_inflation` is in `applied_stroke_transform`-space; `layer_bounds` is path-local and `push_layer` re-applies `multiplied_transform`. // Divide by the smaller axial scale to cover the stroke in both axes after Vello's transform. Skip on a degenerate transform. let (_, smallest_scale) = singular_values(applied_stroke_transform); @@ -1809,17 +2144,35 @@ fn render_vector_item_to_vello( ) }); + // The layers also cover what the effects paint past the content, which reaches a length in the scene's space + let effects_inflation = match singular_values(multiplied_transform) { + (_, smallest_scale) if smallest_scale > 0. => compositing.reach() / smallest_scale, + _ => 0., + }; + let layer_rect_with_effects = |layer_rect: kurbo::Rect| layer_rect.inflate(effects_inflation, effects_inflation); + if needs_blend_layer && let Some((layer_affine, layer_rect)) = layer_geometry { layer = true; - scene.push_layer(peniko::Fill::NonZero, peniko::BlendMode::new(blend_mode, peniko::Compose::SrcOver), opacity, layer_affine, &layer_rect); + scene.push_layer( + peniko::Fill::NonZero, + peniko::BlendMode::new(blend_mode, peniko::Compose::SrcOver), + opacity, + layer_affine, + &layer_rect_with_effects(layer_rect), + ); } // Pushed inside the blend layer so the mask cuts this item's own paint rather than the composited result let mut clip_layers = false; if next_clips && clip_masker.is_none() { - *clip_masker = Some(List::new_from_item(Item::from_parts(element.clone(), item.clone_item_attributes()))); + // The shadows and glows the masker casts don't widen the area its clipped content shows in + let mut masker = Item::from_parts(element.clone(), item.clone_item_attributes()); + masker.remove_attribute::(ATTR_EFFECTS); + *clip_masker = Some(List::new_from_item(masker)); } else if let Some(masker) = clip_masker.as_ref() { if let Some((layer_affine, layer_rect)) = layer_geometry { + let layer_rect = layer_rect_with_effects(layer_rect); + scene.push_layer(peniko::Fill::NonZero, peniko::Mix::Normal, 1., layer_affine, &layer_rect); masker.render_to_vello(scene, parent_transform, &render_params.for_clipper()); scene.push_layer( @@ -1990,76 +2343,79 @@ fn render_vector_item_to_vello( scene.fill_path(peniko::Fill::NonZero, element_affine, outline_color_peniko, None, &outline); } _ => { - if use_layer { - let cloned_element = element.clone(); - - // The mask must draw at full alpha so `SrcOut` fully zeroes the path interior. - // The outer opacity/blend layer (above) handles the user-set opacity. - let mut mask_item = Item::new_from_element(cloned_element) - .with_attribute(ATTR_PATHS, element_paths.clone()) - .with_attribute(ATTR_GEOMETRY_CACHE, item.geometry_cache()) - .with_attribute(ATTR_TRANSFORM, item_transform); - let black_fill = Graphic::ColorList(List::new_from_element(Color::BLACK)); - mask_item.set_attribute(ATTR_APPEARANCE, Appearance::new_single(Coverage::new_fill(), black_fill)); - let vector_list = List::new_from_item(mask_item); - - let bounds = contours_bounding_box(&contours, multiplied_transform).unwrap_or(layer_bounds); - // This branch is gated on `can_draw_aligned_stroke`, which already requires every subpath is closed - let inflation = stroke.map_or(0., |stroke| stroke.max_aabb_inflation(true)); - let (largest_scale, _) = singular_values(applied_stroke_transform); - let quad = Quad::from_box(bounds).inflate(inflation * largest_scale); - let bounds = quad.bounding_box(); - let rect = kurbo::Rect::new(bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y); - - let compose = if stroke.is_some_and(|x| x.align == StrokeAlign::Outside) { - peniko::Compose::SrcOut - } else { - peniko::Compose::SrcIn - }; + let effects_bounds = || layer_geometry.map_or(RenderBoundingBox::None, |(layer_affine, layer_rect)| rect_bounds(layer_affine.transform_rect_bbox(layer_rect))); + compositing.draw_with_effects(scene, effects_bounds, |scene| { + if use_layer { + let cloned_element = element.clone(); + + // The mask must draw at full alpha so `SrcOut` fully zeroes the path interior. + // The outer opacity/blend layer (above) handles the user-set opacity. + let mut mask_item = Item::new_from_element(cloned_element) + .with_attribute(ATTR_PATHS, element_paths.clone()) + .with_attribute(ATTR_GEOMETRY_CACHE, item.geometry_cache()) + .with_attribute(ATTR_TRANSFORM, item_transform); + let black_fill = Graphic::ColorList(List::new_from_element(Color::BLACK)); + mask_item.set_attribute(ATTR_APPEARANCE, Appearance::new_single(Coverage::new_fill(), black_fill)); + let vector_list = List::new_from_item(mask_item); + + let bounds = contours_bounding_box(&contours, multiplied_transform).unwrap_or(layer_bounds); + // This branch is gated on `can_draw_aligned_stroke`, which already requires every subpath is closed + let inflation = stroke.map_or(0., |stroke| stroke.max_aabb_inflation(true)); + let (largest_scale, _) = singular_values(applied_stroke_transform); + let quad = Quad::from_box(bounds).inflate(inflation * largest_scale); + let bounds = quad.bounding_box(); + let rect = kurbo::Rect::new(bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y); + + let compose = if stroke.is_some_and(|x| x.align == StrokeAlign::Outside) { + peniko::Compose::SrcOut + } else { + peniko::Compose::SrcIn + }; - if wants_stroke_below { - scene.push_layer(peniko::Fill::NonZero, peniko::Mix::Normal, 1., kurbo::Affine::IDENTITY, &rect); - vector_list.render_to_vello(scene, parent_transform, &render_params.for_alignment(applied_stroke_transform)); - scene.push_layer(peniko::Fill::NonZero, peniko::BlendMode::new(peniko::Mix::Normal, compose), 1., kurbo::Affine::IDENTITY, &rect); + if wants_stroke_below { + scene.push_layer(peniko::Fill::NonZero, peniko::Mix::Normal, 1., kurbo::Affine::IDENTITY, &rect); + vector_list.render_to_vello(scene, parent_transform, &render_params.for_alignment(applied_stroke_transform)); + scene.push_layer(peniko::Fill::NonZero, peniko::BlendMode::new(peniko::Mix::Normal, compose), 1., kurbo::Affine::IDENTITY, &rect); - do_stroke(scene, 2.); + do_stroke(scene, 2.); - scene.pop_layer(); - scene.pop_layer(); + scene.pop_layer(); + scene.pop_layer(); - do_fill(scene); - } else { - // Fill first (unclipped), then stroke (clipped) above - do_fill(scene); + do_fill(scene); + } else { + // Fill first (unclipped), then stroke (clipped) above + do_fill(scene); - scene.push_layer(peniko::Fill::NonZero, peniko::Mix::Normal, 1., kurbo::Affine::IDENTITY, &rect); - vector_list.render_to_vello(scene, parent_transform, &render_params.for_alignment(applied_stroke_transform)); - scene.push_layer(peniko::Fill::NonZero, peniko::BlendMode::new(peniko::Mix::Normal, compose), 1., kurbo::Affine::IDENTITY, &rect); + scene.push_layer(peniko::Fill::NonZero, peniko::Mix::Normal, 1., kurbo::Affine::IDENTITY, &rect); + vector_list.render_to_vello(scene, parent_transform, &render_params.for_alignment(applied_stroke_transform)); + scene.push_layer(peniko::Fill::NonZero, peniko::BlendMode::new(peniko::Mix::Normal, compose), 1., kurbo::Affine::IDENTITY, &rect); - do_stroke(scene, 2.); + do_stroke(scene, 2.); - scene.pop_layer(); - scene.pop_layer(); - } - } else { - // Non-aligned strokes or open paths: default order behavior - enum Op { - Fill, - Stroke, - } + scene.pop_layer(); + scene.pop_layer(); + } + } else { + // Non-aligned strokes or open paths: default order behavior + enum Op { + Fill, + Stroke, + } - let order = match wants_stroke_below { - true => [Op::Stroke, Op::Fill], - false => [Op::Fill, Op::Stroke], // Default - }; + let order = match wants_stroke_below { + true => [Op::Stroke, Op::Fill], + false => [Op::Fill, Op::Stroke], // Default + }; - for operation in &order { - match operation { - Op::Fill => do_fill(scene), - Op::Stroke => do_stroke(scene, 1.), + for operation in &order { + match operation { + Op::Fill => do_fill(scene), + Op::Stroke => do_stroke(scene, 1.), + } } } - } + }); } } @@ -2366,13 +2722,27 @@ fn render_raster_cpu_item_svg(item: ItemRef<'_, Raster>, render: &mut SvgRe let transform: DAffine2 = item.attribute_cloned_or_default(ATTR_TRANSFORM); let blend_mode_attr: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); - let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.); - let opacity_fill_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY_FILL, 1.); + let compositing = item.compositing(render_params); if image.data.is_empty() { return; } + let filter = compositing.svg_filter(render, || RenderBoundingBox::Rectangle((transform * Quad::from_box([DVec2::ZERO, DVec2::ONE])).bounding_box())); + + // A filter's wrapping group takes the opacity and blend mode instead + let (opacity, blend_mode) = match filter { + Some(_) => (1., BlendMode::default()), + None => (compositing.opacity_without_effects(), blend_mode_attr), + }; + + render_svg_through_filter(render, filter, compositing.opacity, blend_mode_attr, |render| { + render_raster_cpu_image_svg(image, transform, opacity, blend_mode, render, render_params) + }); +} + +/// Emits a CPU raster's image as SVG, either as a placeholder for a canvas or as an embedded PNG. +fn render_raster_cpu_image_svg(image: &Raster, transform: DAffine2, opacity: f32, blend_mode: BlendMode, render: &mut SvgRender, render_params: &RenderParams) { if render_params.to_canvas() { // An image drawn smaller than its pixels is sent at a resolution halved enough times to still cover each screen pixel let document_to_screen = DAffine2::from_scale(DVec2::splat(render_params.canvas_image_scale)); @@ -2401,13 +2771,12 @@ fn render_raster_cpu_item_svg(item: ItemRef<'_, Raster>, render: &mut SvgRe attributes.push("width", size.x.to_string()); attributes.push("height", size.y.to_string()); - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; if opacity < 1. { attributes.push("opacity", opacity.to_string()); } - if blend_mode_attr != BlendMode::default() { - attributes.push("style", blend_mode_attr.render()); + if blend_mode != BlendMode::default() { + attributes.push("style", blend_mode.render()); } }, |render| { @@ -2431,12 +2800,11 @@ fn render_raster_cpu_item_svg(item: ItemRef<'_, Raster>, render: &mut SvgRe attributes.push(ATTR_TRANSFORM, matrix); } - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; if opacity < 1. { attributes.push("opacity", opacity.to_string()); } - if blend_mode_attr != BlendMode::default() { - attributes.push("style", blend_mode_attr.render()); + if blend_mode != BlendMode::default() { + attributes.push("style", blend_mode.render()); } }); } @@ -2450,11 +2818,10 @@ fn render_raster_cpu_item_to_vello(item: ItemRef<'_, Raster>, scene: &mut V } let blend_mode_attr: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); - let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.); - let opacity_fill_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY_FILL, 1.); + let compositing = item.compositing(render_params); let blend_mode = blend_mode_attr.to_peniko(); - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; + let opacity = compositing.opacity; let mut layer = false; let whole_bounds = || match item { @@ -2465,7 +2832,7 @@ fn render_raster_cpu_item_to_vello(item: ItemRef<'_, Raster>, scene: &mut V && let RenderBoundingBox::Rectangle(bounds) = whole_bounds() { let blending = peniko::BlendMode::new(blend_mode, peniko::Compose::SrcOver); - let rect = kurbo::Rect::new(bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y); + let rect = inflate_bounds(bounds, compositing.reach()); scene.push_layer(peniko::Fill::NonZero, blending, opacity, kurbo::Affine::IDENTITY, &rect); layer = true; } @@ -2486,7 +2853,10 @@ fn render_raster_cpu_item_to_vello(item: ItemRef<'_, Raster>, scene: &mut V let image_transform = transform * transform_attribute * DAffine2::from_scale(1. / DVec2::new(image.width as f64, image.height as f64)); let sampler = peniko::ImageSampler::new().with_extend(peniko::Extend::Pad); - scene.draw_raster(image.clone(), sampler, kurbo::Affine::new(image_transform.to_cols_array())); + let item_bounds = || RenderBoundingBox::Rectangle((transform * transform_attribute * Quad::from_box([DVec2::ZERO, DVec2::ONE])).bounding_box()); + compositing.draw_with_effects(scene, item_bounds, |scene| { + scene.draw_raster(image.clone(), sampler, kurbo::Affine::new(image_transform.to_cols_array())) + }); if layer { scene.pop_layer(); @@ -2578,8 +2948,7 @@ impl Render for List> { fn render_raster_gpu_item_to_vello(item: ItemRef<'_, Raster>, scene: &mut VelloSceneBuilder, transform: DAffine2, render_params: &RenderParams) { let Some(raster) = item.element() else { return }; let blend_mode_attr: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); - let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.); - let opacity_fill_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY_FILL, 1.); + let compositing = item.compositing(render_params); let clip_attr: bool = item.attribute_cloned_or_default(ATTR_CLIPPING_MASK); let blend_mode = match render_params.render_mode { RenderMode::Outline => peniko::Mix::Normal, @@ -2592,13 +2961,13 @@ fn render_raster_gpu_item_to_vello(item: ItemRef<'_, Raster>, scene: &mut V ItemRef::ListItem(list, _) => list.bounding_box(transform, true), ItemRef::Item(item) => item.bounding_box(transform, true), }; - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; + let opacity = compositing.opacity; let any_nondefault = blend_mode_attr != BlendMode::default() || opacity < 1. || clip_attr; if (render_params.render_mode != RenderMode::Outline && any_nondefault) && let RenderBoundingBox::Rectangle(bounds) = whole_bounds() { let blending = peniko::BlendMode::new(blend_mode, peniko::Compose::SrcOver); - let rect = kurbo::Rect::new(bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y); + let rect = inflate_bounds(bounds, compositing.reach()); scene.push_layer(peniko::Fill::NonZero, blending, opacity, kurbo::Affine::IDENTITY, &rect); layer = true; } @@ -2621,7 +2990,11 @@ fn render_raster_gpu_item_to_vello(item: ItemRef<'_, Raster>, scene: &mut V let sampler = peniko::ImageSampler::new().with_extend(peniko::Extend::Pad); let image_transform = transform * transform_attribute * DAffine2::from_scale(1. / DVec2::new(width as f64, height as f64)); - scene.draw_texture(raster.texture.clone(), sampler, kurbo::Affine::new(image_transform.to_cols_array())); + + let item_bounds = || RenderBoundingBox::Rectangle((transform * transform_attribute * Quad::from_box([DVec2::ZERO, DVec2::ONE])).bounding_box()); + compositing.draw_with_effects(scene, item_bounds, |scene| { + scene.draw_texture(raster.texture.clone(), sampler, kurbo::Affine::new(image_transform.to_cols_array())) + }); if layer { scene.pop_layer() @@ -2686,10 +3059,9 @@ fn render_color_item_svg(item: ItemRef<'_, Color>, render: &mut SvgRender, rende fn render_color_item_to_vello(item: ItemRef<'_, Color>, scene: &mut VelloSceneBuilder, render_params: &RenderParams) { let Some(color) = item.element() else { return }; let blend_mode_attr: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); - let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.); - let opacity_fill_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY_FILL, 1.); + let compositing = item.compositing(render_params); let blend_mode = blend_mode_attr.to_peniko(); - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; + let opacity = compositing.opacity; let vello_color = SRGBA8::from(*color).to_peniko_color(); @@ -2699,7 +3071,7 @@ fn render_color_item_to_vello(item: ItemRef<'_, Color>, scene: &mut VelloSceneBu layer = true; } - scene.fill_viewport(vello_color, None); + compositing.draw_with_effects(scene, || RenderBoundingBox::Infinite, |scene| scene.fill_viewport(vello_color, None)); if layer { scene.pop_layer(); @@ -2879,12 +3251,11 @@ fn render_gradient_item_to_vello(item: ItemRef<'_, Gradient>, scene: &mut VelloS let gradient_form: GradientForm = item.attribute_cloned_or_default(ATTR_GRADIENT_FORM); let transform: DAffine2 = item.attribute_cloned_or_default(ATTR_TRANSFORM); let blend_mode_attr: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); - let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.); - let opacity_fill_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY_FILL, 1.); + let compositing = item.compositing(render_params); let gradient_transform = parent_transform * transform; let blend_mode = blend_mode_attr.to_peniko(); - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; + let opacity = compositing.opacity; let settings = gradient_settings_from_item(item); let samples = spread_adjusted_samples(gradient, settings, gradient_form); @@ -2910,7 +3281,7 @@ fn render_gradient_item_to_vello(item: ItemRef<'_, Gradient>, scene: &mut VelloS layer = true; } - scene.fill_viewport(&fill, Some(brush_transform)); + compositing.draw_with_effects(scene, || RenderBoundingBox::Infinite, |scene| scene.fill_viewport(&fill, Some(brush_transform))); if layer { scene.pop_layer(); @@ -3170,8 +3541,7 @@ fn render_text_item_svg(item: ItemRef<'_, String>, render: &mut SvgRender, rende } let transform: DAffine2 = item.attribute_cloned_or_default(ATTR_TRANSFORM); - let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.); - let opacity_fill_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY_FILL, 1.); + let compositing = item.compositing(render_params); let blend_mode_attr: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); let font: Resource = { let f: Resource = item.attribute_cloned_or_default(ATTR_FONT); @@ -3184,7 +3554,6 @@ fn render_text_item_svg(item: ItemRef<'_, String>, render: &mut SvgRender, rende let max_height = item.attribute::(ATTR_MAX_HEIGHT).copied().filter(|height| *height > 0.); let letter_tilt: f64 = item.attribute_cloned_or(ATTR_LETTER_TILT, 0.); let align: text_nodes::TextAlign = item.attribute_cloned_or_default(ATTR_TEXT_ALIGN); - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; let typesetting = text_nodes::TypesettingConfig { font_size, @@ -3197,11 +3566,17 @@ fn render_text_item_svg(item: ItemRef<'_, String>, render: &mut SvgRender, rende }; let mut glyph_paths: Vec = Vec::new(); + let mut layout_bounds = RenderBoundingBox::None; text_nodes::TextContext::with_thread_local(|ctx| { let Some(layout) = ctx.layout_text(text, &font, typesetting) else { return }; let tilt_tan = letter_tilt.to_radians().tan(); + // Padded by the font size, since glyphs can overhang their layout box + let alignment_width = max_width.unwrap_or(layout.full_width() as f64); + let padded = Quad::from_box([DVec2::splat(-font_size), DVec2::new(alignment_width, layout.height() as f64) + font_size]); + layout_bounds = RenderBoundingBox::Rectangle((transform * padded).bounding_box()); + text_nodes::for_each_styled_glyph_run(&layout, text, typesetting, |glyph_run, x_offset, space_extra| { draw_glyph_run_to_bezpaths(glyph_run, x_offset, space_extra, tilt_tan, |bez_path| { glyph_paths.push(bez_path.to_svg()); @@ -3213,37 +3588,47 @@ fn render_text_item_svg(item: ItemRef<'_, String>, render: &mut SvgRender, rende return; } - // Wrap all glyph elements in a with the item's transform/opacity/blend-mode. - render.parent_tag( - "g", - |attributes| { - let matrix = format_transform_matrix(transform); - if !matrix.is_empty() { - attributes.push("transform", matrix); - } - if opacity < 1. { - attributes.push("opacity", opacity.to_string()); - } - if blend_mode_attr != BlendMode::default() { - attributes.push("style", blend_mode_attr.render()); - } - }, - |render| { - for path_d in glyph_paths { - render.leaf_tag("path", |attributes| { - attributes.push("d", path_d); - if let RenderMode::Outline = render_params.render_mode { - attributes.push("fill", "none"); - attributes.push("stroke", "black"); - attributes.push("stroke-width", "1"); - } else { - attributes.push("fill", "black"); - attributes.push("fill-rule", "nonzero"); - } - }); - } - }, - ); + let filter = compositing.svg_filter(render, || layout_bounds); + let filtered = filter.is_some(); + + // Wrap all glyph elements in a with the item's transform/opacity/blend-mode, which a filter's wrapping group takes instead + render_svg_through_filter(render, filter, compositing.opacity, blend_mode_attr, |render| { + render.parent_tag( + "g", + |attributes| { + let matrix = format_transform_matrix(transform); + if !matrix.is_empty() { + attributes.push("transform", matrix); + } + if filtered { + return; + } + + let opacity = compositing.opacity_without_effects(); + if opacity < 1. { + attributes.push("opacity", opacity.to_string()); + } + if blend_mode_attr != BlendMode::default() { + attributes.push("style", blend_mode_attr.render()); + } + }, + |render| { + for path_d in glyph_paths { + render.leaf_tag("path", |attributes| { + attributes.push("d", path_d); + if let RenderMode::Outline = render_params.render_mode { + attributes.push("fill", "none"); + attributes.push("stroke", "black"); + attributes.push("stroke-width", "1"); + } else { + attributes.push("fill", "black"); + attributes.push("fill-rule", "nonzero"); + } + }); + } + }, + ); + }); } /// Draws one item of text content into the Vello scene, laying out its glyphs under the item's styling. @@ -3266,9 +3651,8 @@ fn render_text_item_to_vello(item: ItemRef<'_, String>, scene: &mut VelloSceneBu let letter_tilt: f64 = item.attribute_cloned_or(ATTR_LETTER_TILT, 0.); let align: text_nodes::TextAlign = item.attribute_cloned_or_default(ATTR_TEXT_ALIGN); let blend_mode_attr: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE); - let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.); - let opacity_fill_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY_FILL, 1.); - let opacity = (opacity_attr * if render_params.for_mask { 1. } else { opacity_fill_attr }) as f32; + let compositing = item.compositing(render_params); + let opacity = compositing.opacity; let typesetting = text_nodes::TypesettingConfig { font_size, @@ -3285,28 +3669,38 @@ fn render_text_item_to_vello(item: ItemRef<'_, String>, scene: &mut VelloSceneBu text_nodes::TextContext::with_thread_local(|ctx| { let Some(layout) = ctx.layout_text(text, &font, typesetting) else { return }; - let needs_layer = opacity < 1. || blend_mode_attr != BlendMode::default(); - if needs_layer { + let transformed_bounds = || { let alignment_width = max_width.map(|w| w as f32).unwrap_or_else(|| layout.full_width()); - let blending = peniko::BlendMode::new(blend_mode_attr.to_peniko(), peniko::Compose::SrcOver); let padding = font_size; let bounds = kurbo::Rect::new(-padding, -padding, alignment_width as f64 + padding, layout.height() as f64 + padding); - let transformed_bounds = affine.transform_rect_bbox(bounds); - scene.push_layer(peniko::Fill::NonZero, blending, opacity, kurbo::Affine::IDENTITY, &transformed_bounds); + affine.transform_rect_bbox(bounds) + }; + + let needs_layer = opacity < 1. || blend_mode_attr != BlendMode::default(); + if needs_layer { + let blending = peniko::BlendMode::new(blend_mode_attr.to_peniko(), peniko::Compose::SrcOver); + let reach = compositing.reach(); + scene.push_layer(peniko::Fill::NonZero, blending, opacity, kurbo::Affine::IDENTITY, &transformed_bounds().inflate(reach, reach)); } let tilt_tan = letter_tilt.to_radians().tan(); - text_nodes::for_each_styled_glyph_run(&layout, text, typesetting, |glyph_run, x_offset, space_extra| { - draw_glyph_run_to_bezpaths(glyph_run, x_offset, space_extra, tilt_tan, |bez_path| { - if let RenderMode::Outline = render_params.render_mode { - let (outline_stroke, outline_color) = get_outline_styles(render_params); - scene.stroke(&outline_stroke, affine, outline_color, None, bez_path); - } else { - scene.fill(peniko::Fill::NonZero, affine, peniko::Color::BLACK, None, bez_path); - } - }); - }); + compositing.draw_with_effects( + scene, + || rect_bounds(transformed_bounds()), + |scene| { + text_nodes::for_each_styled_glyph_run(&layout, text, typesetting, |glyph_run, x_offset, space_extra| { + draw_glyph_run_to_bezpaths(glyph_run, x_offset, space_extra, tilt_tan, |bez_path| { + if let RenderMode::Outline = render_params.render_mode { + let (outline_stroke, outline_color) = get_outline_styles(render_params); + scene.stroke(&outline_stroke, affine, outline_color, None, bez_path); + } else { + scene.fill(peniko::Fill::NonZero, affine, peniko::Color::BLACK, None, bez_path); + } + }); + }); + }, + ); if needs_layer { scene.pop_layer(); @@ -3540,4 +3934,49 @@ mod tests { assert_eq!(halvings(4.), [0]); assert_eq!(halvings(0.), [0]); } + + #[test] + fn svg_effects_filters_measure_lengths_in_the_document() { + use graphic_types::EffectOperation; + + let shadow = EffectOperation::DropShadow { + color: Color::BLACK, + offset: DVec2::new(4., 0.), + blur: 2., + spread: 0., + }; + let mut defs = String::new(); + + // A user space drawn at twice the document's scale halves every length measured in it, so the shadow reaches 5 units past the content + write_svg_effects_filter( + &mut defs, + &Effects::from_operations([shadow]), + 1., + [DVec2::ZERO, DVec2::splat(10.)], + DAffine2::from_scale(DVec2::splat(2.)), + ) + .unwrap(); + + assert!(defs.contains(r#"dx="2" dy="0""#), "{defs}"); + assert!(defs.contains(r#"stdDeviation="1""#), "{defs}"); + assert!(defs.contains(r#"x="-5" y="-5" width="20" height="20""#), "{defs}"); + } + + #[test] + fn svg_effects_filters_end_with_the_image_shown() { + use graphic_types::EffectOperation; + + let blur = EffectOperation::Blur { + radius: 3., + box_blur: false, + gamma: false, + }; + let mut defs = String::new(); + write_svg_effects_filter(&mut defs, &Effects::from_operations([blur]), 0.5, [DVec2::ZERO, DVec2::ONE], DAffine2::IDENTITY).unwrap(); + + // The fill fades a copy of the content, which is blurred in linear light and passed through as the filter's last result + assert!(defs.contains(r#""#), "{defs}"); + assert!(defs.contains(r#"in="effect-1" stdDeviation="3" color-interpolation-filters="linearRGB""#), "{defs}"); + assert!(defs.ends_with(r#""#), "{defs}"); + } } diff --git a/node-graph/libraries/rendering/src/vello_scene_builder.rs b/node-graph/libraries/rendering/src/vello_scene_builder.rs index 2695af4acf7..f96e7b1f796 100644 --- a/node-graph/libraries/rendering/src/vello_scene_builder.rs +++ b/node-graph/libraries/rendering/src/vello_scene_builder.rs @@ -1,3 +1,5 @@ +use glam::{DMat2, UVec2}; +use graphic_types::Effects; use graphic_types::raster_types::{CPU, Raster, Texture}; use kurbo::{Affine, BezPath, PathEl, Point, Rect, Shape, Stroke}; use std::borrow::Cow; @@ -8,9 +10,11 @@ use vello_gpu::{Image, ImageSource, PaintType, TextureId, TextureRegion}; /// Tolerance for turning shapes other than paths into paths, matching Vello's own. const SHAPE_TOLERANCE: f64 = 0.1; +/// The texture size an effects group's area rounds up to a multiple of, so a group moving across renders keeps reusing the same pooled textures. +const EFFECTS_SIZE_CLASS: u32 = 64; + /// Builds a Vello GPU scene of a fixed size, recording the images it draws so the renderer can supply their textures at render time. -#[derive(Debug)] -pub struct VelloSceneBuilder { +pub struct VelloSceneBuilder<'a> { scene: vello_gpu::Scene, /// Places the drawn content in the scene, applied on top of every transform it's drawn with. view: Affine, @@ -20,6 +24,18 @@ pub struct VelloSceneBuilder { /// The largest width or height an image's texture can have, beyond which images are drawn from a downscaled texture. max_texture_size: u32, pushed: Vec, + /// Renders effects groups as they're drawn. A scene drawn ahead of its render has none, so it draws effects groups without their effects. + effects_renderer: Option<&'a mut dyn EffectsRenderer>, +} + +/// Renders the content of a scene's effects groups offscreen and carries out their effects, while the scene is drawn. +pub trait EffectsRenderer: Send { + /// Whether the GPU can carry out effects, without which effects groups draw their content faded by their fill. + fn renders_effects(&self) -> bool; + + /// Renders the content `draw` draws, placed by `view`, into a premultiplied texture of `size`, returning a texture of the same size with `effects` carried out on it. + /// `lengths_to_pixels` maps the effects' lengths onto the texture's pixels, and `fill` fades the content but not the shadows and glows cast from it. + fn render_effects(&mut self, size: UVec2, view: Affine, effects: &Effects, fill: f32, lengths_to_pixels: DMat2, draw: &mut dyn FnMut(&mut VelloSceneBuilder)) -> wgpu::Texture; } /// The pixels behind an image the scene draws. @@ -31,6 +47,8 @@ pub enum SceneImage { Raster(Raster), /// Straight-alpha pixels in a GPU texture. Texture(Texture), + /// Premultiplied pixels of an effects group's result, rendered for this scene. + Rendered(wgpu::Texture), } #[derive(Clone, PartialEq, Eq, Hash, Debug)] @@ -38,6 +56,7 @@ enum ImageKey { Blob(u64), Raster(u64), Texture(Texture), + Rendered(wgpu::Texture), } #[derive(Debug)] @@ -46,7 +65,17 @@ enum Pushed { Clip, } -impl VelloSceneBuilder { +impl std::fmt::Debug for VelloSceneBuilder<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("VelloSceneBuilder") + .field("scene", &self.scene) + .field("view", &self.view) + .field("images", &self.images) + .finish_non_exhaustive() + } +} + +impl<'a> VelloSceneBuilder<'a> { pub fn new(width: u16, height: u16, view: Affine, max_texture_size: u32) -> Self { Self::from_vello(vello_gpu::Scene::new(width, height), view, max_texture_size) } @@ -61,9 +90,16 @@ impl VelloSceneBuilder { image_ids: HashMap::new(), max_texture_size, pushed: Vec::new(), + effects_renderer: None, } } + /// Renders the effects groups drawn into the scene through `renderer`. + pub fn with_effects_renderer(mut self, renderer: &'a mut dyn EffectsRenderer) -> Self { + self.effects_renderer = Some(renderer); + self + } + pub fn width(&self) -> u16 { self.scene.width() } @@ -191,6 +227,52 @@ impl VelloSceneBuilder { self.draw_image_source(source, sampler, transform * texture_to_image, texture_size); } + /// Draws the content `draw` draws with `effects` carried out on it, given the area it covers in the space of the drawn content, or `None` if unbounded. + /// `fill` fades the content but not the shadows and glows cast from it. Rendering a split scene calls `draw` again for each part. + pub fn draw_effects(&mut self, effects: &Effects, fill: f32, bounds: Option, draw: &mut dyn FnMut(&mut VelloSceneBuilder)) { + if !self.effects_renderer.as_ref().is_some_and(|renderer| renderer.renders_effects()) { + self.push_viewport_layer(peniko::Mix::Normal, fill); + draw(self); + self.pop_layer(); + return; + } + + let [a, b, c, d, _, _] = self.view.as_coeffs(); + let lengths_to_device = DMat2::from_cols_array(&[a, b, c, d]); + let reach = effects.reach() * lengths_to_device.determinant().abs().sqrt(); + + // The content reaching into the viewport, rounded out to whole pixels + let viewport = Rect::new(0., 0., self.width() as f64, self.height() as f64); + let content_bounds = bounds.map_or(viewport, |bounds| self.view.transform_rect_bbox(bounds)); + let region = content_bounds.inflate(reach, reach).intersect(viewport.inflate(reach, reach)).expand(); + if !(region.width() > 0. && region.height() > 0.) { + return; + } + + // A region too large for one texture renders at a lower resolution, which the blurs and shadows that make it so large hide. + // Its size rounds up to a size class so a group that moves between renders keeps reusing the same textures. + let max_size = self.max_texture_size.min(u16::MAX as u32); + let resolution = (max_size as f64 / region.width().max(region.height())).min(1.); + let size_along = |length: f64| ((length * resolution).ceil() as u32).next_multiple_of(EFFECTS_SIZE_CLASS).min(max_size); + let size = UVec2::new(size_along(region.width()), size_along(region.height())); + let origin = region.origin().to_vec2(); + + let content_view = Affine::scale(resolution) * Affine::translate(-origin) * self.view; + let Some(renderer) = self.effects_renderer.as_deref_mut() else { return }; + let result = renderer.render_effects(size, content_view, effects, fill, lengths_to_device * resolution, draw); + + // Draw the result where the content was + self.images.push(SceneImage::Rendered(result)); + let id = TextureId(self.images.len() as u64 - 1); + self.scene.set_transform(Affine::translate(origin) * Affine::scale(1. / resolution)); + self.scene.set_paint(PaintType::Image(Image { + image: ImageSource::external_texture(id, TextureRegion::Full, true), + sampler: peniko::ImageSampler::new(), + })); + self.scene.set_paint_transform(Affine::IDENTITY); + self.scene.fill_rect(&Rect::new(0., 0., size.x as f64, size.y as f64)); + } + fn draw_image_source(&mut self, source: ImageSource, sampler: peniko::ImageSampler, transform: Affine, (width, height): (u32, u32)) { self.scene.set_transform(self.view * transform); self.scene.set_paint(PaintType::Image(Image { image: source, sampler })); @@ -222,6 +304,7 @@ impl VelloSceneBuilder { SceneImage::Pixels(data) => (data.width, data.height), SceneImage::Raster(raster) => (raster.width, raster.height), SceneImage::Texture(texture) => (texture.width(), texture.height()), + SceneImage::Rendered(texture) => (texture.width(), texture.height()), }; if width == 0 || height == 0 { return None; @@ -231,6 +314,7 @@ impl VelloSceneBuilder { SceneImage::Pixels(data) => ImageKey::Blob(data.data.id()), SceneImage::Raster(raster) => ImageKey::Raster(raster.buffer_id()), SceneImage::Texture(texture) => ImageKey::Texture(texture.clone()), + SceneImage::Rendered(texture) => ImageKey::Rendered(texture.clone()), }; let id = *self.image_ids.entry(key).or_insert_with(|| { self.images.push(image); diff --git a/node-graph/libraries/wgpu-executor/Cargo.toml b/node-graph/libraries/wgpu-executor/Cargo.toml index e45a852e468..ff153f7b613 100644 --- a/node-graph/libraries/wgpu-executor/Cargo.toml +++ b/node-graph/libraries/wgpu-executor/Cargo.toml @@ -16,6 +16,8 @@ workspace = true [dependencies] # Local dependencies core-types = { workspace = true } +graphic-types = { workspace = true } +raster-kernels = { workspace = true } raster-types = { workspace = true, features = ["wgpu"] } graphene-application-io = { workspace = true, features = ["wgpu"] } rendering = { workspace = true } diff --git a/node-graph/libraries/wgpu-executor/src/lib.rs b/node-graph/libraries/wgpu-executor/src/lib.rs index 087703265c0..9c4a8538610 100644 --- a/node-graph/libraries/wgpu-executor/src/lib.rs +++ b/node-graph/libraries/wgpu-executor/src/lib.rs @@ -3,6 +3,7 @@ mod context; mod pipeline; mod texture_cache; pub mod texture_conversion; +mod vello_effects; mod vello_render; use crate::texture_cache::TextureCache; @@ -177,7 +178,7 @@ impl WgpuExecutor { } pub fn with_context(context: WgpuContext) -> Option { - let vello_renderer = VelloRenderer::new(&context.device); + let vello_renderer = VelloRenderer::new(&context.device, &context.adapter); let texture_cache = TextureCache::new(TEXTURE_CACHE_SIZE); diff --git a/node-graph/libraries/wgpu-executor/src/vello_effects.rs b/node-graph/libraries/wgpu-executor/src/vello_effects.rs new file mode 100644 index 00000000000..53d6bcd8292 --- /dev/null +++ b/node-graph/libraries/wgpu-executor/src/vello_effects.rs @@ -0,0 +1,592 @@ +//! The GPU passes that carry out an item's effects on its content, between rendering the content offscreen and drawing the result back. + +use bytemuck::{Pod, Zeroable}; +use core_types::Color; +use glam::{DMat2, DVec2, UVec2}; +use graphic_types::{EffectOperation, Effects}; +use raster_kernels::effect_kernels::{BlurKernel, MAX_KERNEL_REACH, NEGLIGIBLE_LENGTH}; +use std::collections::HashMap; +use std::sync::{Mutex, PoisonError}; + +/// The format of the images the passes produce, with the precision to keep soft gradients smooth. +pub(crate) const WORKING_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float; + +/// The format of the silhouettes shadows and glows are cast from, which hold only coverage. +const COVERAGE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::R16Float; + +/// The most taps one kernel pass weighs, matching the length of the shader's array, which holds a kernel of [`MAX_KERNEL_REACH`] texels to each side in pairs. +const MAX_TAPS: usize = 1 + 2 * MAX_KERNEL_REACH.div_ceil(2); + +/// How many renders a pooled texture survives unused before it is freed. +const UNUSED_TEXTURE_RENDER_LIMIT: u64 = 3; + +/// The shader's parameters, laid out to match its `Parameters` struct. +#[repr(C)] +#[derive(Clone, Copy, Pod, Zeroable)] +struct Parameters { + target_to_uv: [f32; 2], + step: [f32; 2], + offset: [f32; 2], + amount: f32, + tap_count: u32, + color: [f32; 4], + taps: [[f32; 4]; MAX_TAPS], +} + +impl Default for Parameters { + fn default() -> Self { + Self::zeroed() + } +} + +/// The kernel's taps for a texture halved `levels` times, each the distance and weight of a symmetric pair of taps that sample between two texels. +fn kernel_taps(kernel: BlurKernel, levels: u32) -> Vec<[f32; 4]> { + let weights = kernel.weights(levels); + + // Each tap between a pair of texels samples them in the ratio of their weights, through bilinear filtering + let mut taps = vec![[0., weights[0] as f32, 0., 0.]]; + for (pair_index, pair) in weights[1..].chunks(2).enumerate() { + let near_distance = (1 + 2 * pair_index) as f64; + let (distance, weight) = match *pair { + [near, far] => ((near_distance * near + (near_distance + 1.) * far) / (near + far), near + far), + [near] => (near_distance, near), + _ => continue, + }; + taps.push([distance as f32, weight as f32, 0., 0.]); + taps.push([-distance as f32, weight as f32, 0., 0.]); + } + + taps +} + +/// The pipelines of the effect passes, each built the first time it is used. +pub(crate) struct EffectPasses { + shader: wgpu::ShaderModule, + bind_group_layout: wgpu::BindGroupLayout, + pipeline_layout: wgpu::PipelineLayout, + sampler: wgpu::Sampler, + pipelines: Mutex>, + /// Whether the GPU can render into the passes' formats, which WebGL2 can't without `EXT_color_buffer_float`. + pub(crate) renders_working_formats: bool, +} + +impl EffectPasses { + pub(crate) fn new(device: &wgpu::Device, adapter: &wgpu::Adapter) -> Self { + let renders_working_formats = [WORKING_FORMAT, COVERAGE_FORMAT] + .into_iter() + .all(|format| adapter.get_texture_format_features(format).allowed_usages.contains(wgpu::TextureUsages::RENDER_ATTACHMENT)); + if !renders_working_formats { + log::warn!("This GPU can't render into the formats layer effects need, so content draws without its effects"); + } + + let shader = device.create_shader_module(wgpu::include_wgsl!("vello_effects.wgsl")); + + let texture_entry = |binding| wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { filterable: true }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }; + let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("vello_effects_bind_group_layout"), + entries: &[ + texture_entry(0), + texture_entry(1), + texture_entry(2), + wgpu::BindGroupLayoutEntry { + binding: 3, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 4, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }, + ], + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("vello_effects_pipeline_layout"), + bind_group_layouts: &[Some(&bind_group_layout)], + immediate_size: 0, + }); + + // Sampling past the edges repeats them, which is transparent since the content's offscreen area reaches past it by the effects' reach + let sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("vello_effects_sampler"), + address_mode_u: wgpu::AddressMode::ClampToEdge, + address_mode_v: wgpu::AddressMode::ClampToEdge, + mag_filter: wgpu::FilterMode::Linear, + min_filter: wgpu::FilterMode::Linear, + ..Default::default() + }); + + Self { + shader, + bind_group_layout, + pipeline_layout, + sampler, + pipelines: Mutex::new(HashMap::new()), + renders_working_formats, + } + } + + fn pipeline(&self, device: &wgpu::Device, entry_point: &'static str, format: wgpu::TextureFormat) -> wgpu::RenderPipeline { + let mut pipelines = self.pipelines.lock().unwrap_or_else(PoisonError::into_inner); + + pipelines + .entry((entry_point, format)) + .or_insert_with(|| { + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(entry_point), + layout: Some(&self.pipeline_layout), + vertex: wgpu::VertexState { + module: &self.shader, + entry_point: Some("vertex"), + compilation_options: Default::default(), + buffers: &[], + }, + fragment: Some(wgpu::FragmentState { + module: &self.shader, + entry_point: Some(entry_point), + compilation_options: Default::default(), + targets: &[Some(wgpu::ColorTargetState { + format, + blend: None, + write_mask: wgpu::ColorWrites::ALL, + })], + }), + primitive: wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, + ..Default::default() + }, + depth_stencil: None, + multisample: wgpu::MultisampleState::default(), + multiview_mask: None, + cache: None, + }) + }) + .clone() + } + + /// Carries out `effects` on `content`, a texture of premultiplied pixels, returning the result in a pooled texture of [`WORKING_FORMAT`] the same size. + /// `lengths_to_pixels` maps the effects' lengths onto the texture's pixels, and `fill` fades the content but not the shadows and glows cast from it. + #[expect(clippy::too_many_arguments)] + pub(crate) fn apply( + &self, + device: &wgpu::Device, + queue: &wgpu::Queue, + textures: &mut EffectTextures, + content: &wgpu::Texture, + effects: &Effects, + fill: f32, + lengths_to_pixels: DMat2, + ) -> wgpu::Texture { + let mut recorder = PassRecorder { + passes: self, + device, + encoder: device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("vello_effects") }), + textures, + size: UVec2::new(content.width(), content.height()), + }; + let length_scale = lengths_to_pixels.determinant().abs().sqrt(); + + // The content at full strength, which casts every silhouette, and the content faded by the fill, which shows in the result + let mut full = recorder.scale(content, 1.); + let mut filled = (fill < 1.).then(|| recorder.scale(content, fill)); + // The shadows and glows stacked beneath the content + let mut beneath: Option = None; + + for effect in effects.operations() { + match effect { + EffectOperation::Blur { radius, box_blur, gamma } => { + let radius = radius * length_scale; + if radius < NEGLIGIBLE_LENGTH { + continue; + } + + // What lies beneath blurs together with the content + if let Some(beneath) = beneath.take() { + if let Some(filled) = &mut filled { + let composited = recorder.composite_beneath(filled, &beneath, &full); + recorder.replace(filled, composited); + } + let composited = recorder.composite_beneath(&full, &beneath, &full); + recorder.replace(&mut full, composited); + recorder.textures.release(beneath); + } + + let kernel = if box_blur { BlurKernel::Box { radius } } else { BlurKernel::Gaussian { sigma: radius } }; + full = recorder.blur(full, kernel, !gamma); + filled = filled.map(|filled| recorder.blur(filled, kernel, !gamma)); + } + EffectOperation::DropShadow { color, offset, blur, spread } => { + let coverage = recorder.cast(&full, lengths_to_pixels * offset, blur * length_scale, spread * length_scale, false); + beneath = Some(recorder.accumulate(beneath, &coverage, color)); + recorder.textures.release(coverage); + } + EffectOperation::OuterGlow { color, blur, spread } => { + let coverage = recorder.cast(&full, DVec2::ZERO, blur * length_scale, spread * length_scale, false); + beneath = Some(recorder.accumulate(beneath, &coverage, color)); + recorder.textures.release(coverage); + } + EffectOperation::InnerShadow { color, offset, blur, spread } => { + let coverage = recorder.cast(&full, lengths_to_pixels * offset, blur * length_scale, spread * length_scale, true); + if let Some(filled) = &mut filled { + let composited = recorder.composite_within(filled, &coverage, &full, color); + recorder.replace(filled, composited); + } + let composited = recorder.composite_within(&full, &coverage, &full, color); + recorder.replace(&mut full, composited); + recorder.textures.release(coverage); + } + } + } + + let result = match (filled, beneath) { + (filled, Some(beneath)) => { + let result = recorder.composite_beneath(filled.as_ref().unwrap_or(&full), &beneath, &full); + recorder.textures.release(beneath); + recorder.textures.release(full); + if let Some(filled) = filled { + recorder.textures.release(filled); + } + result + } + (Some(filled), None) => { + recorder.textures.release(full); + filled + } + (None, None) => full, + }; + + queue.submit([recorder.encoder.finish()]); + result + } +} + +/// A color as the premultiplied, gamma-encoded channels the passes work with. +fn premultiplied_gamma_channels(color: Color) -> [f32; 4] { + let [red, green, blue, alpha] = color.to_gamma_srgb_channels(); + [red * alpha, green * alpha, blue * alpha, alpha] +} + +/// Records the passes of one effects group, whose images all share one size. +struct PassRecorder<'a> { + passes: &'a EffectPasses, + device: &'a wgpu::Device, + encoder: wgpu::CommandEncoder, + textures: &'a mut EffectTextures, + size: UVec2, +} + +impl PassRecorder<'_> { + /// Runs the shader's `entry_point` into a new pooled texture of `format` and `size`, reading `source` and optionally the `secondary` and `content` inputs. + #[expect(clippy::too_many_arguments)] + fn run( + &mut self, + entry_point: &'static str, + format: wgpu::TextureFormat, + size: UVec2, + source: &wgpu::Texture, + secondary: Option<&wgpu::Texture>, + content: Option<&wgpu::Texture>, + parameters: Parameters, + ) -> wgpu::Texture { + use wgpu::util::DeviceExt; + + let target = self.textures.acquire(self.device, size, format); + let parameters = Parameters { + target_to_uv: [1. / size.x as f32, 1. / size.y as f32], + ..parameters + }; + + let view = |texture: &wgpu::Texture| texture.create_view(&wgpu::TextureViewDescriptor::default()); + let (source_view, secondary_view, content_view) = (view(source), view(secondary.unwrap_or(source)), view(content.unwrap_or(source))); + let parameters_buffer = self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("vello_effects_parameters"), + contents: bytemuck::bytes_of(¶meters), + usage: wgpu::BufferUsages::UNIFORM, + }); + let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("vello_effects_bind_group"), + layout: &self.passes.bind_group_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(&source_view), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView(&secondary_view), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: wgpu::BindingResource::TextureView(&content_view), + }, + wgpu::BindGroupEntry { + binding: 3, + resource: wgpu::BindingResource::Sampler(&self.passes.sampler), + }, + wgpu::BindGroupEntry { + binding: 4, + resource: parameters_buffer.as_entire_binding(), + }, + ], + }); + + let pipeline = self.passes.pipeline(self.device, entry_point, format); + let target_view = view(&target); + let mut pass = self.encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some(entry_point), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &target_view, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), + store: wgpu::StoreOp::Store, + }, + depth_slice: None, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + pass.set_pipeline(&pipeline); + pass.set_bind_group(0, &bind_group, &[]); + pass.draw(0..3, 0..1); + drop(pass); + + target + } + + /// Puts `next` in place of the texture in `slot`, returning the old one to the pool. + fn replace(&mut self, slot: &mut wgpu::Texture, next: wgpu::Texture) { + self.textures.release(std::mem::replace(slot, next)); + } + + /// A copy of `source` in the working format, multiplied by `amount`. + fn scale(&mut self, source: &wgpu::Texture, amount: f32) -> wgpu::Texture { + self.run("scale", WORKING_FORMAT, self.size, source, None, None, Parameters { amount, ..Default::default() }) + } + + /// The coverage of the content's silhouette (or of the area outside it, if `inverse`), cast by `offset`, grown by `spread` and softened by `blur`, all in pixels. + fn cast(&mut self, content: &wgpu::Texture, offset: DVec2, blur: f64, spread: f64, inverse: bool) -> wgpu::Texture { + let offset = offset / self.size.as_dvec2(); + let parameters = Parameters { + offset: [offset.x as f32, offset.y as f32], + ..Default::default() + }; + let entry_point = if inverse { "cast_inverse_silhouette" } else { "cast_silhouette" }; + let mut coverage = self.run(entry_point, COVERAGE_FORMAT, self.size, content, None, None, parameters); + + if spread >= NEGLIGIBLE_LENGTH { + coverage = self.dilate(coverage, spread); + } + if blur >= NEGLIGIBLE_LENGTH { + coverage = self.blur(coverage, BlurKernel::Gaussian { sigma: blur }, false); + } + + coverage + } + + /// Stacks a silhouette's coverage, tinted `color`, over what lies `beneath` the content so far. + fn accumulate(&mut self, beneath: Option, coverage: &wgpu::Texture, color: Color) -> wgpu::Texture { + let parameters = Parameters { + color: premultiplied_gamma_channels(color), + ..Default::default() + }; + + let Some(beneath) = beneath else { + return self.run("tint", WORKING_FORMAT, self.size, coverage, None, None, parameters); + }; + let accumulated = self.run("accumulate", WORKING_FORMAT, self.size, &beneath, Some(coverage), None, parameters); + self.textures.release(beneath); + + accumulated + } + + /// The image with a silhouette's coverage, tinted `color`, placed over it within the full-strength content. + fn composite_within(&mut self, image: &wgpu::Texture, coverage: &wgpu::Texture, full_content: &wgpu::Texture, color: Color) -> wgpu::Texture { + let parameters = Parameters { + color: premultiplied_gamma_channels(color), + ..Default::default() + }; + self.run("composite_within", WORKING_FORMAT, self.size, image, Some(coverage), Some(full_content), parameters) + } + + /// The image with the shadows and glows `beneath` placed under it, knocked out by the full-strength content. + fn composite_beneath(&mut self, image: &wgpu::Texture, beneath: &wgpu::Texture, full_content: &wgpu::Texture) -> wgpu::Texture { + self.run("composite_beneath", WORKING_FORMAT, self.size, image, Some(beneath), Some(full_content), Parameters::default()) + } + + /// Grows the shapes in `texture` by `distance` pixels along each axis, in a square like SVG's `feMorphology`. + fn dilate(&mut self, texture: wgpu::Texture, distance: f64) -> wgpu::Texture { + // Steps that double, then the remainder, so each pass grows the shapes by up to as much as all the passes before it + let mut steps = Vec::new(); + let (mut covered, mut step) = (0., 1.); + while covered + step <= distance { + steps.push(step); + covered += step; + step *= 2.; + } + if distance - covered >= NEGLIGIBLE_LENGTH { + steps.push(distance - covered); + } + + let format = texture.format(); + let mut texture = texture; + for step in steps { + for axis in [DVec2::X, DVec2::Y] { + let step = axis * step / self.size.as_dvec2(); + let parameters = Parameters { + step: [step.x as f32, step.y as f32], + ..Default::default() + }; + let dilated = self.run("dilate", format, self.size, &texture, None, None, parameters); + self.replace(&mut texture, dilated); + } + } + + texture + } + + /// Blurs `texture` by `kernel` along both axes, in linear light if `linear`. + /// Kernels too wide for one pass run on a texture halved enough times for them to fit, then doubled back to full size. O(width × height) per pass. + fn blur(&mut self, texture: wgpu::Texture, kernel: BlurKernel, linear: bool) -> wgpu::Texture { + let format = texture.format(); + let mut texture = texture; + + if linear { + let decoded = self.run("decode_to_linear", format, self.size, &texture, None, None, Parameters::default()); + self.replace(&mut texture, decoded); + } + + let levels = kernel.levels(); + let size = self.size; + let level_size = |level: u32| UVec2::new(size.x.div_ceil(1 << level), size.y.div_ceil(1 << level)); + + for level in 1..=levels { + let halved = self.run("resample", format, level_size(level), &texture, None, None, Parameters::default()); + self.replace(&mut texture, halved); + } + + let taps = kernel_taps(kernel, levels); + let mut tap_array = [[0.; 4]; MAX_TAPS]; + tap_array[..taps.len()].copy_from_slice(&taps); + for axis in [DVec2::X, DVec2::Y] { + let step = axis / level_size(levels).as_dvec2(); + let parameters = Parameters { + step: [step.x as f32, step.y as f32], + tap_count: taps.len() as u32, + taps: tap_array, + ..Default::default() + }; + let convolved = self.run("convolve", format, level_size(levels), &texture, None, None, parameters); + self.replace(&mut texture, convolved); + } + + for level in (0..levels).rev() { + let doubled = self.run("resample", format, level_size(level), &texture, None, None, Parameters::default()); + self.replace(&mut texture, doubled); + } + + if linear { + let encoded = self.run("encode_to_gamma", format, self.size, &texture, None, None, Parameters::default()); + self.replace(&mut texture, encoded); + } + + texture + } +} + +/// Textures for the effect passes and the content they work on, kept between renders for reuse at the same sizes. +#[derive(Default)] +pub(crate) struct EffectTextures { + /// Each unused texture with the render it was last used in. + free: Vec<(wgpu::Texture, u64)>, + render_count: u64, +} + +impl EffectTextures { + pub(crate) fn acquire(&mut self, device: &wgpu::Device, size: UVec2, format: wgpu::TextureFormat) -> wgpu::Texture { + let size = size.max(UVec2::ONE); + let matches = |texture: &wgpu::Texture| texture.width() == size.x && texture.height() == size.y && texture.format() == format; + if let Some(index) = self.free.iter().position(|(texture, _)| matches(texture)) { + return self.free.swap_remove(index).0; + } + + device.create_texture(&wgpu::TextureDescriptor { + label: Some("vello_effects_texture"), + size: wgpu::Extent3d { + width: size.x, + height: size.y, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }) + } + + /// Returns a texture to the pool once the passes recorded so far are done with it, since later passes are recorded and submitted after them. + pub(crate) fn release(&mut self, texture: wgpu::Texture) { + self.free.push((texture, self.render_count)); + } + + /// Frees the textures no recent render has used. + pub(crate) fn finish_render(&mut self) { + let render_count = self.render_count; + self.free.retain(|(_, last_used)| render_count - last_used < UNUSED_TEXTURE_RENDER_LIMIT); + self.render_count += 1; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The sum of the weights of all taps, which must be one so blurring keeps the total brightness. + fn total_weight(taps: &[[f32; 4]]) -> f32 { + taps.iter().map(|tap| tap[1]).sum() + } + + #[test] + fn kernels_keep_brightness_and_fit_one_pass() { + let kernels = [0.3, 1., 2.5, 7.9, 8., 12., 100., 4000.] + .into_iter() + .flat_map(|length| [BlurKernel::Gaussian { sigma: length }, BlurKernel::Box { radius: length }]); + + for kernel in kernels { + let taps = kernel_taps(kernel, kernel.levels()); + + assert!(taps.len() <= MAX_TAPS, "{kernel:?} needs {} taps", taps.len()); + assert!((total_weight(&taps) - 1.).abs() < 1e-5, "{kernel:?} weighs {}", total_weight(&taps)); + } + } + + #[test] + fn merged_taps_sample_between_the_texels_they_weigh() { + // A box of half width two weighs five texels evenly, so its outer pair merges into taps one and a half texels out + let taps = kernel_taps(BlurKernel::Box { radius: 2. }, 0); + + assert_eq!(taps.len(), 3); + assert!((taps[0][1] - 0.2).abs() < 1e-6); + assert!((taps[1][0] - 1.5).abs() < 1e-6 && (taps[1][1] - 0.4).abs() < 1e-6); + assert!((taps[2][0] + 1.5).abs() < 1e-6); + } +} diff --git a/node-graph/libraries/wgpu-executor/src/vello_effects.wgsl b/node-graph/libraries/wgpu-executor/src/vello_effects.wgsl new file mode 100644 index 00000000000..a6e4fcb7013 --- /dev/null +++ b/node-graph/libraries/wgpu-executor/src/vello_effects.wgsl @@ -0,0 +1,147 @@ +// The passes that carry out an item's effects on the pixels of its content, which are premultiplied with gamma-encoded color like the pixels Vello GPU blends + +struct Parameters { + // Converts a target pixel's position into the normalized coordinates the inputs are sampled at + target_to_uv: vec2, + // One step along the axis a kernel runs, in normalized coordinates + step: vec2, + // How far a silhouette is cast, in normalized coordinates + offset: vec2, + // The factor the scale pass multiplies by + amount: f32, + tap_count: u32, + // The premultiplied color a silhouette is tinted + color: vec4, + // Each kernel tap's distance in steps (x) and weight (y) + taps: array, 25>, +} + +@group(0) @binding(0) var source: texture_2d; +@group(0) @binding(1) var secondary: texture_2d; +@group(0) @binding(2) var content: texture_2d; +@group(0) @binding(3) var linear_sampler: sampler; +@group(0) @binding(4) var parameters: Parameters; + +// One triangle that covers the whole target +@vertex +fn vertex(@builtin(vertex_index) index: u32) -> @builtin(position) vec4 { + let corner = vec2(f32((index << 1u) & 2u), f32(index & 2u)); + return vec4(corner * 2. - 1., 0., 1.); +} + +fn sample_source(uv: vec2) -> vec4 { + return textureSampleLevel(source, linear_sampler, uv, 0.); +} + +// A separable kernel along one axis, whose taps sit between texels to weight two of them at once +@fragment +fn convolve(@builtin(position) position: vec4) -> @location(0) vec4 { + let uv = position.xy * parameters.target_to_uv; + + var sum = vec4(0.); + for (var i = 0u; i < parameters.tap_count; i++) { + let tap = parameters.taps[i]; + sum += sample_source(uv + parameters.step * tap.x) * tap.y; + } + return sum; +} + +// Halving a texture samples the corner shared by each block of four texels, averaging them, and doubling it interpolates between texels +@fragment +fn resample(@builtin(position) position: vec4) -> @location(0) vec4 { + return sample_source(position.xy * parameters.target_to_uv); +} + +// Grows the shapes by one step along an axis, so steps that double each time grow them by any distance in a few passes +@fragment +fn dilate(@builtin(position) position: vec4) -> @location(0) vec4 { + let uv = position.xy * parameters.target_to_uv; + return max(sample_source(uv), max(sample_source(uv - parameters.step), sample_source(uv + parameters.step))); +} + +// The coverage of the content's silhouette, shifted by the offset +@fragment +fn cast_silhouette(@builtin(position) position: vec4) -> @location(0) vec4 { + return vec4(sample_source(position.xy * parameters.target_to_uv - parameters.offset).a); +} + +// The coverage of the area outside the content's silhouette, shifted by the offset +@fragment +fn cast_inverse_silhouette(@builtin(position) position: vec4) -> @location(0) vec4 { + return vec4(1. - sample_source(position.xy * parameters.target_to_uv - parameters.offset).a); +} + +// A silhouette's coverage in its color +@fragment +fn tint(@builtin(position) position: vec4) -> @location(0) vec4 { + return parameters.color * textureLoad(source, vec2(position.xy), 0).r; +} + +// Stacks a tinted silhouette over the shadows and glows beneath the content +@fragment +fn accumulate(@builtin(position) position: vec4) -> @location(0) vec4 { + let pixel = vec2(position.xy); + let beneath = textureLoad(source, pixel, 0); + let tinted = parameters.color * textureLoad(secondary, pixel, 0).r; + + return tinted + beneath * (1. - tinted.a); +} + +// Places a tinted silhouette over the image, within the content at full strength so it stays visible over a faded fill +@fragment +fn composite_within(@builtin(position) position: vec4) -> @location(0) vec4 { + let pixel = vec2(position.xy); + let image = textureLoad(source, pixel, 0); + let tinted = parameters.color * textureLoad(secondary, pixel, 0).r; + let full_content = textureLoad(content, pixel, 0); + + return tinted * full_content.a + image * (1. - tinted.a); +} + +// Places the shadows and glows beneath the image, knocked out by the content at full strength so a faded fill doesn't reveal them +@fragment +fn composite_beneath(@builtin(position) position: vec4) -> @location(0) vec4 { + let pixel = vec2(position.xy); + let image = textureLoad(source, pixel, 0); + let beneath = textureLoad(secondary, pixel, 0); + let full_content = textureLoad(content, pixel, 0); + + return image + beneath * (1. - full_content.a); +} + +@fragment +fn scale(@builtin(position) position: vec4) -> @location(0) vec4 { + return textureLoad(source, vec2(position.xy), 0) * parameters.amount; +} + +@fragment +fn decode_to_linear(@builtin(position) position: vec4) -> @location(0) vec4 { + let color = textureLoad(source, vec2(position.xy), 0); + if color.a <= 0. { + return vec4(0.); + } + + return vec4(srgb_to_linear(max(color.rgb / color.a, vec3(0.))) * color.a, color.a); +} + +@fragment +fn encode_to_gamma(@builtin(position) position: vec4) -> @location(0) vec4 { + let color = textureLoad(source, vec2(position.xy), 0); + if color.a <= 0. { + return vec4(0.); + } + + return vec4(linear_to_srgb(max(color.rgb / color.a, vec3(0.))) * color.a, color.a); +} + +fn srgb_to_linear(srgb: vec3) -> vec3 { + let low = srgb / 12.92; + let high = pow((srgb + 0.055) / 1.055, vec3(2.4)); + return select(high, low, srgb <= vec3(0.04045)); +} + +fn linear_to_srgb(linear: vec3) -> vec3 { + let low = linear * 12.92; + let high = 1.055 * pow(linear, vec3(1. / 2.4)) - 0.055; + return select(high, low, linear <= vec3(0.0031308)); +} diff --git a/node-graph/libraries/wgpu-executor/src/vello_render.rs b/node-graph/libraries/wgpu-executor/src/vello_render.rs index 01bc289eeaf..1004eb7ed39 100644 --- a/node-graph/libraries/wgpu-executor/src/vello_render.rs +++ b/node-graph/libraries/wgpu-executor/src/vello_render.rs @@ -1,6 +1,8 @@ -use glam::UVec2; +use crate::vello_effects::{EffectPasses, EffectTextures}; +use glam::{DMat2, UVec2}; +use graphic_types::Effects; use raster_types::WeakCPU; -use rendering::vello_scene_builder::{SceneImage, VelloSceneBuilder, image_downscale_factor, image_texture_size}; +use rendering::vello_scene_builder::{EffectsRenderer, SceneImage, VelloSceneBuilder, image_downscale_factor, image_texture_size}; use std::collections::HashMap; use std::sync::{Mutex, PoisonError}; use vello_gpu::kurbo::Affine; @@ -29,20 +31,37 @@ pub(crate) struct VelloRenderer { /// Locked only while a scene is rendered and submitted, so one canvas drawing its scene on the CPU doesn't hold up another's render. gpu: Mutex<(Renderer, Resources)>, alpha_conversion: AlphaConversion, + effect_passes: EffectPasses, } /// A stream of frames rendered through the shared Vello renderer, such as the document or the overlays, keeping what its frames reuse. #[derive(Default)] pub struct VelloCanvas { - /// The Vello GPU scene of the last render, handed out again to keep its allocations. - spare_scene: Option, + /// Scenes to draw into, one for each depth of effects groups nested in a render, reused between renders to keep their allocations. + spare_scenes: Vec, /// The premultiplied texture Vello GPU renders into before conversion to straight alpha, kept while the render size stays the same. premultiplied_target: Option, images: ImageTextures, + effect_textures: EffectTextures, +} + +/// Whether a render leaves its target with the straight alpha the rest of Graphite expects, or the premultiplied alpha the effect passes take. +#[derive(Clone, Copy, PartialEq, Eq)] +enum TargetAlpha { + Straight, + Premultiplied, +} + +/// A render in progress: the shared renderer and GPU, and the canvas whose resources it draws with. +struct RenderContext<'a> { + renderer: &'a VelloRenderer, + canvas: &'a mut VelloCanvas, + device: &'a wgpu::Device, + queue: &'a Queue, } impl VelloRenderer { - pub(crate) fn new(device: &wgpu::Device) -> Self { + pub(crate) fn new(device: &wgpu::Device, adapter: &wgpu::Adapter) -> Self { let render_target_config = RenderTargetConfig { format: TEXTURE_FORMAT, width: 1, @@ -60,6 +79,7 @@ impl VelloRenderer { Self { gpu: Mutex::new(Renderer::new_with(device, &render_target_config, settings)), alpha_conversion: AlphaConversion::new(device), + effect_passes: EffectPasses::new(device, adapter), } } @@ -81,10 +101,15 @@ impl VelloRenderer { } canvas.images.render_count += 1; - if let Err(error) = self.render_once(canvas, device, queue, draw, view, size, target) { - self.render_halves(canvas, device, queue, draw, view, size, target, error, 1); - } + let mut context = RenderContext { + renderer: self, + canvas, + device, + queue, + }; + context.render_area(draw, view, size, target, TargetAlpha::Straight); canvas.images.free_unused(); + canvas.effect_textures.finish_render(); Ok(()) } @@ -94,27 +119,34 @@ impl VelloRenderer { pub(crate) fn render_scene(&self, canvas: &mut VelloCanvas, device: &wgpu::Device, queue: &Queue, scene: VelloSceneBuilder, target: &wgpu::Texture) { canvas.images.render_count += 1; let max_texture_size = scene.max_texture_size(); - if let Err(error) = self.submit(canvas, device, queue, scene.finish(), max_texture_size, target) { + let mut context = RenderContext { + renderer: self, + canvas, + device, + queue, + }; + if let Err(error) = context.submit(scene.finish(), max_texture_size, target, TargetAlpha::Straight) { log::error!("Vello GPU failed to render: {error}"); clear(device, queue, target); } canvas.images.free_unused(); + canvas.effect_textures.finish_render(); + } +} + +impl RenderContext<'_> { + /// Renders the scene `draw` draws into `target`, a texture of `size`, splitting its area into halves while it holds more content than one render can. + fn render_area(&mut self, draw: &mut dyn FnMut(&mut VelloSceneBuilder), view: Affine, size: UVec2, target: &wgpu::Texture, target_alpha: TargetAlpha) { + if let Err(error) = self.render_once(draw, view, size, target, target_alpha) { + self.render_halves(draw, view, size, target, target_alpha, error, 1); + } } /// Renders each half of the area of `target`, a texture of `size`, after rendering it whole failed with `error`, splitting again each half that still holds too much. #[expect(clippy::too_many_arguments)] - fn render_halves( - &self, - canvas: &mut VelloCanvas, - device: &wgpu::Device, - queue: &Queue, - draw: &mut dyn FnMut(&mut VelloSceneBuilder), - view: Affine, - size: UVec2, - target: &wgpu::Texture, - error: RenderError, - split_depth: u32, - ) { + fn render_halves(&mut self, draw: &mut dyn FnMut(&mut VelloSceneBuilder), view: Affine, size: UVec2, target: &wgpu::Texture, target_alpha: TargetAlpha, error: RenderError, split_depth: u32) { + let (device, queue) = (self.device, self.queue); + // Each half covers less of the scene, so it has less content and smaller layers to hold let too_much_to_hold = matches!( error, @@ -142,7 +174,7 @@ impl VelloRenderer { wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST, ); let half_view = Affine::translate((-(offset.x as f64), -(offset.y as f64))) * view; - let rendered = self.render_once(canvas, device, queue, draw, half_view, half_size, &half_target); + let rendered = self.render_once(draw, half_view, half_size, &half_target, target_alpha); (offset, half_size, half_view, half_target, rendered) }); @@ -166,7 +198,7 @@ impl VelloRenderer { for (offset, half_size, half_view, half_target, rendered) in halves { if let Err(half_error) = rendered { - self.render_halves(canvas, device, queue, draw, half_view, half_size, &half_target, half_error, split_depth + 1); + self.render_halves(draw, half_view, half_size, &half_target, target_alpha, half_error, split_depth + 1); } let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("vello_gpu_split_copy") }); @@ -182,43 +214,29 @@ impl VelloRenderer { } } - /// Renders the scene `draw` draws, with its content placed by `view`, into `target`, a texture of `size`, in a single Vello GPU render. - #[expect(clippy::too_many_arguments)] - fn render_once( - &self, - canvas: &mut VelloCanvas, - device: &wgpu::Device, - queue: &Queue, - draw: &mut dyn FnMut(&mut VelloSceneBuilder), - view: Affine, - size: UVec2, - target: &wgpu::Texture, - ) -> Result<(), RenderError> { + /// Renders the scene `draw` draws into `target`, a texture of `size`, in a single Vello GPU render. + fn render_once(&mut self, draw: &mut dyn FnMut(&mut VelloSceneBuilder), view: Affine, size: UVec2, target: &wgpu::Texture, target_alpha: TargetAlpha) -> Result<(), RenderError> { let (width, height) = (size.x as u16, size.y as u16); - let max_texture_size = device.limits().max_texture_dimension_2d; + let max_texture_size = self.device.limits().max_texture_dimension_2d; - let mut scene = canvas.spare_scene.take().unwrap_or_else(|| vello_gpu::Scene::new(width, height)); + // Draw the scene for this render's size and placement, which renders its effects groups as they're drawn + let mut scene = self.canvas.spare_scenes.pop().unwrap_or_else(|| vello_gpu::Scene::new(width, height)); scene.reset_and_resize(width, height); - let mut builder = VelloSceneBuilder::from_vello(scene, view, max_texture_size); + let mut builder = VelloSceneBuilder::from_vello(scene, view, max_texture_size).with_effects_renderer(self); draw(&mut builder); + let drawn = builder.finish(); - self.submit(canvas, device, queue, builder.finish(), max_texture_size, target) + self.submit(drawn, max_texture_size, target, target_alpha) } /// Renders a drawn scene, with the images it draws, into `target`, a texture of the scene's size, in a single Vello GPU render. - fn submit( - &self, - canvas: &mut VelloCanvas, - device: &wgpu::Device, - queue: &Queue, - (scene, images): (vello_gpu::Scene, Vec), - max_texture_size: u32, - target: &wgpu::Texture, - ) -> Result<(), RenderError> { + fn submit(&mut self, (scene, images): (vello_gpu::Scene, Vec), max_texture_size: u32, target: &wgpu::Texture, target_alpha: TargetAlpha) -> Result<(), RenderError> { + let device = self.device; let (width, height) = (scene.width(), scene.height()); + let size = UVec2::new(width as u32, height as u32); // Locked from here to the submission, so drawing a scene doesn't hold up other uses of the queue - let queue = queue.lock(); + let queue = self.queue.lock(); // Each render submits its own encoder, since Vello GPU stages its uploads for the next submission let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("vello_gpu_render") }); @@ -226,31 +244,32 @@ impl VelloRenderer { // Supply the textures of the images the scene draws, under the IDs it drew them with let mut texture_bindings = TextureBindings::new(); for (id, image) in images.iter().enumerate() { - if let Some(view) = canvas.images.prepare(device, &queue, &mut encoder, &self.alpha_conversion, image, max_texture_size) { + if let Some(view) = self.canvas.images.prepare(device, &queue, &mut encoder, &self.renderer.alpha_conversion, image, max_texture_size) { texture_bindings.insert(TextureId(id as u64), view); } } - // Render premultiplied, then convert to the straight alpha the rest of Graphite expects - let size = UVec2::new(width as u32, height as u32); - let premultiplied_target = match &canvas.premultiplied_target { - Some(texture) if texture.width() == size.x && texture.height() == size.y => texture.clone(), - _ => { + // Vello GPU renders premultiplied, which is converted to straight alpha unless the target wants it premultiplied + let target_view = target.create_view(&wgpu::TextureViewDescriptor::default()); + let premultiplied_view = match (target_alpha, &self.canvas.premultiplied_target) { + (TargetAlpha::Premultiplied, _) => target_view.clone(), + (TargetAlpha::Straight, Some(texture)) if texture.width() == size.x && texture.height() == size.y => texture.create_view(&wgpu::TextureViewDescriptor::default()), + (TargetAlpha::Straight, _) => { let texture = create_texture( device, "vello_gpu_premultiplied_target", size, wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, ); - canvas.premultiplied_target = Some(texture.clone()); - texture + let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); + self.canvas.premultiplied_target = Some(texture); + view } }; - let premultiplied_view = premultiplied_target.create_view(&wgpu::TextureViewDescriptor::default()); let target_init = TargetInit::Clear(ClearSettings::Viewport { color: peniko::Color::TRANSPARENT }); // Vello GPU stages this render's uploads into resources every render shares, so another can't stage its own until this one is submitted - let mut gpu = self.gpu.lock().unwrap_or_else(PoisonError::into_inner); + let mut gpu = self.renderer.gpu.lock().unwrap_or_else(PoisonError::into_inner); let (renderer, resources) = &mut *gpu; let rendered = renderer.render( &scene, @@ -265,20 +284,43 @@ impl VelloRenderer { target_init, ); - if rendered.is_ok() { - let target_view = target.create_view(&wgpu::TextureViewDescriptor::default()); - self.alpha_conversion.unpremultiply(device, &mut encoder, &premultiplied_view, &target_view); + if rendered.is_ok() && target_alpha == TargetAlpha::Straight { + self.renderer.alpha_conversion.unpremultiply(device, &mut encoder, &premultiplied_view, &target_view); } // Submitted even after a failure, since it also prepares the textures of the scene's images queue.submit([encoder.finish()]); drop((gpu, queue)); - canvas.spare_scene = Some(scene); + + // Once the render that draws them is submitted, its scene and effects results are free to reuse + self.canvas.spare_scenes.push(scene); + for image in images { + if let SceneImage::Rendered(texture) = image { + self.canvas.effect_textures.release(texture); + } + } rendered } } +impl EffectsRenderer for RenderContext<'_> { + fn renders_effects(&self) -> bool { + self.renderer.effect_passes.renders_working_formats + } + + fn render_effects(&mut self, size: UVec2, view: Affine, effects: &Effects, fill: f32, lengths_to_pixels: DMat2, draw: &mut dyn FnMut(&mut VelloSceneBuilder)) -> wgpu::Texture { + let content_texture = self.canvas.effect_textures.acquire(self.device, size, TEXTURE_FORMAT); + self.render_area(draw, view, size, &content_texture, TargetAlpha::Premultiplied); + + let effect_textures = &mut self.canvas.effect_textures; + let result = (self.renderer.effect_passes).apply(self.device, &self.queue.lock(), effect_textures, &content_texture, effects, fill, lengths_to_pixels); + effect_textures.release(content_texture); + + result + } +} + /// Leaves `target` transparent. fn clear(device: &wgpu::Device, queue: &Queue, target: &wgpu::Texture) { let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("vello_gpu_clear") }); @@ -325,6 +367,8 @@ impl ImageTextures { SceneImage::Pixels(data) => ImageKey::Blob(data.data.id()), SceneImage::Raster(raster) => ImageKey::Raster(raster.buffer_id()), SceneImage::Texture(texture) => ImageKey::Texture((**texture).clone()), + // Effects results are premultiplied already + SceneImage::Rendered(texture) => return Some(texture.create_view(&wgpu::TextureViewDescriptor::default())), }; if !self.textures.contains_key(&key) { @@ -341,6 +385,7 @@ impl ImageTextures { let (pixels, width, height) = raster.to_flat_u8(); upload_pixels(device, queue, &pixels, width, height, peniko::ImageAlphaType::Alpha, max_texture_size)? } + SceneImage::Rendered(_) => return None, SceneImage::Texture(texture) => create_texture( device, "vello_gpu_premultiplied_image", @@ -350,7 +395,7 @@ impl ImageTextures { }; let raster = match image { SceneImage::Raster(raster) => Some(raster.downgrade()), - SceneImage::Pixels(_) | SceneImage::Texture(_) => None, + SceneImage::Pixels(_) | SceneImage::Texture(_) | SceneImage::Rendered(_) => None, }; self.textures.insert(key.clone(), ImageTexture { texture, last_used: 0, raster }); } 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 297c37be1f7..c48822b9b4a 100644 --- a/node-graph/libraries/wgpu-executor/src/vello_render/tests.rs +++ b/node-graph/libraries/wgpu-executor/src/vello_render/tests.rs @@ -274,6 +274,253 @@ fn degenerate_strokes_render() { let Some(_) = render(size, draw) else { return }; } +/// Renders an opaque white square from (`min`, `min`) to (`max`, `max`) through `effects` with `fill`. +fn render_square_with_effects(effects: Vec, fill: f32, min: f64, max: f64, size: UVec2) -> Option> { + let square = Rect::new(min, min, max, max); + let effects = graphic_types::Effects::from_operations(effects); + + render(size, |scene| { + scene.draw_effects(&effects, fill, Some(square), &mut |scene| scene.fill(Fill::NonZero, Affine::IDENTITY, Color::WHITE, None, &square)); + }) +} + +/// The standard normal cumulative distribution, from the Abramowitz and Stegun approximation of the error function, accurate to 1e-7. +fn normal_cdf(x: f64) -> f64 { + let z = x.abs() / std::f64::consts::SQRT_2; + let t = 1. / (1. + 0.327_591_1 * z); + let polynomial = t * (0.254_829_592 + t * (-0.284_496_736 + t * (1.421_413_741 + t * (-1.453_152_027 + t * 1.061_405_429)))); + let erf = 1. - polynomial * (-z * z).exp(); + + if x >= 0. { 0.5 * (1. + erf) } else { 0.5 * (1. - erf) } +} + +#[test] +fn gaussian_blur_follows_the_normal_distribution() { + use graphic_types::EffectOperation; + + let size = UVec2::new(512, 256); + // Small kernels run directly, and larger ones on halved textures + for sigma in [2., 6., 20.] { + let blur = EffectOperation::Blur { + radius: sigma, + box_blur: false, + gamma: false, + }; + // A square much larger than the blur, so across its left edge at x = 100, on a row far from its other edges, the coverage is the one-dimensional profile + let Some(pixels) = render_square_with_effects(vec![blur], 1., 100., 400., size) else { return }; + + let mut worst = 0_f64; + for x in 40..160 { + let expected = normal_cdf((x as f64 + 0.5 - 100.) / sigma); + let actual = pixel(&pixels, size, x, 200)[3] as f64 / 255.; + worst = worst.max((expected - actual).abs()); + } + assert!(worst < 0.03, "σ {sigma} strays from the normal distribution by {worst}"); + } +} + +#[test] +fn shadows_and_glows_fall_where_they_should() { + use core_types::Color as GraphicColor; + use glam::DVec2; + use graphic_types::EffectOperation; + + let size = UVec2::new(128, 128); + let black = GraphicColor::BLACK; + + // A hard drop shadow offset down and right shows past the square's bottom right corner, beneath it + let shadow = EffectOperation::DropShadow { + color: black, + offset: DVec2::new(10., 10.), + blur: 0., + spread: 0., + }; + let Some(pixels) = render_square_with_effects(vec![shadow.clone()], 1., 30., 80., size) else { + return; + }; + assert_eq!(pixel(&pixels, size, 50, 50), [255, 255, 255, 255], "the content covers its shadow"); + assert_eq!(pixel(&pixels, size, 85, 85), [0, 0, 0, 255], "the shadow shows past the content"); + assert_eq!(pixel(&pixels, size, 25, 25)[3], 0, "no shadow before the offset"); + + // With no fill, the content vanishes and its shadow stays knocked out where the content would be + let Some(pixels) = render_square_with_effects(vec![shadow], 0., 30., 80., size) else { return }; + assert_eq!(pixel(&pixels, size, 50, 50)[3], 0, "the shadow is knocked out by the faded content"); + assert_eq!(pixel(&pixels, size, 85, 85), [0, 0, 0, 255], "the shadow stays at full strength"); + + // An inner shadow darkens the content's top left edges from inside and stays within it + let inner_shadow = EffectOperation::InnerShadow { + color: black, + offset: DVec2::new(6., 6.), + blur: 0., + spread: 0., + }; + let Some(pixels) = render_square_with_effects(vec![inner_shadow], 1., 30., 80., size) else { + return; + }; + assert_eq!(pixel(&pixels, size, 32, 50), [0, 0, 0, 255], "inside the left edge"); + assert_eq!(pixel(&pixels, size, 50, 50), [255, 255, 255, 255], "deep inside"); + assert_eq!(pixel(&pixels, size, 78, 50), [255, 255, 255, 255], "inside the right edge, away from the offset"); + assert_eq!(pixel(&pixels, size, 25, 50)[3], 0, "outside"); + + // A glow grown by its spread surrounds the content evenly + let glow = EffectOperation::OuterGlow { + color: GraphicColor::from_rgbaf32_unchecked(1., 0., 0., 1.), + blur: 0., + spread: 5., + }; + let Some(pixels) = render_square_with_effects(vec![glow], 1., 30., 80., size) else { return }; + for (x, y) in [(27, 55), (82, 55), (55, 27), (55, 82)] { + assert_eq!(pixel(&pixels, size, x, y), [255, 0, 0, 255], "glow at ({x}, {y})"); + } + for (x, y) in [(23, 55), (87, 55)] { + assert_eq!(pixel(&pixels, size, x, y)[3], 0, "past the glow at ({x}, {y})"); + } +} + +#[test] +fn later_shadows_stack_over_earlier_ones_beneath_the_content() { + use core_types::Color as GraphicColor; + use glam::DVec2; + use graphic_types::EffectOperation; + + let size = UVec2::new(128, 128); + let shadow = |color, offset: f64| EffectOperation::DropShadow { + color, + offset: DVec2::splat(offset), + blur: 0., + spread: 0., + }; + + // Both shadows are cast from the content rather than from each other, and the later one is on top where they overlap + let effects = vec![shadow(GraphicColor::BLACK, 10.), shadow(GraphicColor::from_rgbaf32_unchecked(0., 0., 1., 1.), 5.)]; + let Some(pixels) = render_square_with_effects(effects, 1., 30., 80., size) else { return }; + assert_eq!(pixel(&pixels, size, 82, 82), [0, 0, 255, 255], "the later shadow is on top"); + assert_eq!(pixel(&pixels, size, 88, 88), [0, 0, 0, 255], "the earlier shadow reaches further"); + assert_eq!(pixel(&pixels, size, 93, 93)[3], 0, "no shadow of a shadow"); +} + +#[test] +fn nested_effects_groups_render_inside_out() { + use core_types::Color as GraphicColor; + use glam::DVec2; + use graphic_types::{EffectOperation, Effects}; + + let size = UVec2::new(128, 128); + let square = Rect::new(30., 30., 80., 80.); + + // A hard shadow around a group whose content is itself a blurred square + let shadow = Effects::from_operations([EffectOperation::DropShadow { + color: GraphicColor::BLACK, + offset: DVec2::new(20., 0.), + blur: 0., + spread: 0., + }]); + let blur = Effects::from_operations([EffectOperation::Blur { + radius: 3., + box_blur: false, + gamma: false, + }]); + let draw = |scene: &mut VelloSceneBuilder| { + scene.draw_effects(&shadow, 1., Some(square.inflate(12., 12.)), &mut |scene| { + scene.draw_effects(&blur, 1., Some(square), &mut |scene| scene.fill(Fill::NonZero, Affine::IDENTITY, Color::WHITE, None, &square)); + }); + }; + + let Some(pixels) = render(size, draw) else { return }; + let edge = pixel(&pixels, size, 30, 55); + assert!((100..=155).contains(&edge[3]), "the blur softens the edge to half coverage, not {edge:?}"); + // Ten pixels past the content's blurred edge, where the shadow is cast from ten pixels inside it + let shadow = pixel(&pixels, size, 90, 55); + assert!(shadow[3] >= 250 && shadow[0] <= 5, "the shadow is cast by the blurred group, not {shadow:?}"); +} + +#[test] +fn cpu_evaluated_effects_match_the_gpu_passes() { + use core_types::Color as GraphicColor; + use core_types::list::{ATTR_EFFECTS, Item}; + use core_types::{ATTR_OPACITY_FILL, ATTR_TRANSFORM}; + use glam::DVec2; + use graphic_types::raster_types::{Image, Raster}; + use graphic_types::{EffectOperation, Effects}; + use raster_kernels::evaluate_raster_effects; + + let size = UVec2::new(160, 160); + let (min, max) = (50, 100); + let shadow_color = GraphicColor::from_rgbaf32_unchecked(0.1, 0.2, 0.8, 0.75); + let blur = |radius, box_blur, gamma| EffectOperation::Blur { radius, box_blur, gamma }; + + let cases = [ + vec![blur(3., false, false)], + vec![blur(20., false, true)], + vec![blur(5.5, true, false)], + vec![EffectOperation::DropShadow { + color: shadow_color, + offset: DVec2::new(6.5, -3.), + blur: 4., + spread: 2., + }], + vec![EffectOperation::InnerShadow { + color: shadow_color, + offset: DVec2::new(4., 4.), + blur: 3., + spread: 0., + }], + vec![ + EffectOperation::OuterGlow { + color: shadow_color, + blur: 6., + spread: 3., + }, + blur(2., false, false), + ], + ]; + + for effects in cases { + for fill in [1., 0.5] { + let Some(gpu) = render_square_with_effects(effects.clone(), fill, min as f64, max as f64, size) else { + return; + }; + + // The same square as a raster covering the render one pixel to one pixel + let mut image = Image::new(size.x, size.y, GraphicColor::TRANSPARENT); + for y in min..max { + for x in min..max { + image.data[(y * size.x + x) as usize] = GraphicColor::WHITE; + } + } + let item = Item::new_from_element(Raster::new_cpu(image)) + .with_attribute(ATTR_TRANSFORM, DAffine2::from_scale(size.as_dvec2())) + .with_attribute(ATTR_EFFECTS, Effects::from_operations(effects.clone())) + .with_attribute(ATTR_OPACITY_FILL, fill as f64); + let evaluated = evaluate_raster_effects(item); + + // Reads the evaluated image at the center of each render pixel through its grown placement + let placement: DAffine2 = evaluated.attribute_cloned_or_default(ATTR_TRANSFORM); + let raster = evaluated.element(); + let document_to_pixels = DAffine2::from_scale(DVec2::new(raster.width as f64, raster.height as f64)) * placement.inverse(); + + let mut worst = 0; + for y in 0..size.y { + for x in 0..size.x { + let point = document_to_pixels.transform_point2(DVec2::new(x as f64 + 0.5, y as f64 + 0.5)).floor(); + let cpu = raster.data[(point.y as u32 * raster.width + point.x as u32) as usize]; + let [red, green, blue, alpha] = cpu.to_gamma_srgb_channels(); + let cpu = [red * alpha, green * alpha, blue * alpha, alpha].map(|channel| channel * 255.); + + // Compared premultiplied, so a nearly transparent pixel's color can't dominate + let gpu = pixel(&gpu, size, x, y); + let gpu_alpha = gpu[3] as f32 / 255.; + for channel in 0..4 { + let gpu_channel = if channel == 3 { gpu[3] as f32 } else { gpu[channel] as f32 * gpu_alpha }; + worst = worst.max((cpu[channel] - gpu_channel).abs().round() as u32); + } + } + } + assert!(worst <= 2, "{effects:?} at fill {fill} differs from the GPU by up to {worst} levels"); + } + } +} + #[test] fn minified_images_average_their_pixels() { use raster_types::{Image, Raster}; diff --git a/node-graph/nodes/blending/src/effects.rs b/node-graph/nodes/blending/src/effects.rs new file mode 100644 index 00000000000..57fbc160449 --- /dev/null +++ b/node-graph/nodes/blending/src/effects.rs @@ -0,0 +1,148 @@ +use core_types::list::Item; +use core_types::{Color, Ctx}; +use glam::DVec2; +use graphic_types::raster_types::{CPU, GPU, Raster}; +use graphic_types::{Effect, EffectOperation, Graphic, Vector, stamp_effect}; +use vector_types::Gradient; + +/// Blurs the input graphics, whether vector shapes, images, or text. +#[node_macro::node(category("Raster: Filter"), icon("NodeBlur"), version(2))] +fn blur( + _: impl Ctx, + /// The content to blur. + #[implementations(Graphic, Vector, Raster, Raster, Color, Gradient, String)] + content: Item, + /// How far the blur spreads each point of the content. + #[unit(" px")] + #[range] + #[hard(0..)] + #[soft(..100)] + radius: Item, + /// Use a box kernel instead of a circular Gaussian kernel, which averages evenly and leaves boxy artifacts. + box_blur: Item, + /// Opt to incorrectly blend the colors in gamma space for compatibility with the results from other software. + gamma: Item, +) -> Item { + let mut content = content; + stamp_effect( + &mut content, + Effect::new(&EffectOperation::Blur { + radius: *radius.element(), + box_blur: *box_blur.element(), + gamma: *gamma.element(), + }), + ); + content +} + +/// Casts a soft shadow of the input graphics' silhouette beneath them. +#[node_macro::node(category("Raster: Effect"), icon("NodeDropShadow"))] +fn drop_shadow( + _: impl Ctx, + /// The content casting the shadow. + #[implementations(Graphic, Vector, Raster, Raster, Color, Gradient, String)] + content: Item, + /// The shadow's color, with its opacity. + #[default("#00000080")] + color: Item, + /// How far the shadow sits from the content. + #[unit(" px")] + #[default(4., 4.)] + offset: Item, + /// How soft the shadow's edges are. + #[unit(" px")] + #[range] + #[hard(0..)] + #[soft(..100)] + #[default(4.)] + blur: Item, + /// How far the shadow grows past the content's silhouette before it softens. + #[unit(" px")] + #[hard(0..)] + spread: Item, +) -> Item { + let mut content = content; + stamp_effect( + &mut content, + Effect::new(&EffectOperation::DropShadow { + color: *color.element(), + offset: *offset.element(), + blur: *blur.element(), + spread: *spread.element(), + }), + ); + content +} + +/// Casts a soft shadow inward from the edges of the input graphics, as if they were cut out of the surface. +#[node_macro::node(category("Raster: Effect"), icon("NodeInnerShadow"))] +fn inner_shadow( + _: impl Ctx, + /// The content the shadow falls inside. + #[implementations(Graphic, Vector, Raster, Raster, Color, Gradient, String)] + content: Item, + /// The shadow's color, with its opacity. + #[default("#00000080")] + color: Item, + /// How far the shadow sits from the edges it falls from. + #[unit(" px")] + #[default(4., 4.)] + offset: Item, + /// How soft the shadow's edges are. + #[unit(" px")] + #[range] + #[hard(0..)] + #[soft(..100)] + #[default(4.)] + blur: Item, + /// How far the shadow reaches in from the content's edges before it softens. + #[unit(" px")] + #[hard(0..)] + spread: Item, +) -> Item { + let mut content = content; + stamp_effect( + &mut content, + Effect::new(&EffectOperation::InnerShadow { + color: *color.element(), + offset: *offset.element(), + blur: *blur.element(), + spread: *spread.element(), + }), + ); + content +} + +/// Surrounds the input graphics with a soft glow of their silhouette. +#[node_macro::node(category("Raster: Effect"), icon("NodeOuterGlow"))] +fn outer_glow( + _: impl Ctx, + /// The content to glow around. + #[implementations(Graphic, Vector, Raster, Raster, Color, Gradient, String)] + content: Item, + /// The glow's color, with its opacity. + #[default("#ffffbebf")] + color: Item, + /// How soft the glow's edges are. + #[unit(" px")] + #[range] + #[hard(0..)] + #[soft(..100)] + #[default(8.)] + blur: Item, + /// How far the glow grows past the content's silhouette before it softens. + #[unit(" px")] + #[hard(0..)] + spread: Item, +) -> Item { + let mut content = content; + stamp_effect( + &mut content, + Effect::new(&EffectOperation::OuterGlow { + color: *color.element(), + blur: *blur.element(), + spread: *spread.element(), + }), + ); + content +} diff --git a/node-graph/nodes/blending/src/lib.rs b/node-graph/nodes/blending/src/lib.rs index 406c99d1021..ab030517e89 100644 --- a/node-graph/nodes/blending/src/lib.rs +++ b/node-graph/nodes/blending/src/lib.rs @@ -1,3 +1,5 @@ +pub mod effects; + use core_types::list::Item; use core_types::{ATTR_BLEND_MODE, ATTR_CLIPPING_MASK, ATTR_OPACITY, ATTR_OPACITY_FILL, BlendMode, Color, Ctx}; use graphic_types::Graphic; diff --git a/node-graph/nodes/gstd/Cargo.toml b/node-graph/nodes/gstd/Cargo.toml index 28b7d967b9c..332f3651b00 100644 --- a/node-graph/nodes/gstd/Cargo.toml +++ b/node-graph/nodes/gstd/Cargo.toml @@ -45,6 +45,7 @@ wgpu-executor = { workspace = true } core-types = { workspace = true } vector-types = { workspace = true } graphic-types = { workspace = true } +raster-kernels = { workspace = true } blending-nodes = { workspace = true } text-nodes = { workspace = true } transform-nodes = { workspace = true } diff --git a/node-graph/nodes/gstd/src/lib.rs b/node-graph/nodes/gstd/src/lib.rs index 422267f438c..63e34520c3d 100644 --- a/node-graph/nodes/gstd/src/lib.rs +++ b/node-graph/nodes/gstd/src/lib.rs @@ -13,7 +13,7 @@ pub use graphene_application_io as application_io; pub use graphene_core; pub use graphene_core::debug; pub use graphic_nodes; -pub use graphic_types::{Appearance, Artboard, Cover, CoverPlacement, Coverage, Graphic, Vector, stamp_coverage}; +pub use graphic_types::{Appearance, Artboard, Cover, CoverPlacement, Coverage, Effect, EffectKind, EffectOperation, Effects, Graphic, Vector, stamp_coverage}; pub use math_nodes; pub use path_bool_nodes; pub use raster_nodes; diff --git a/node-graph/nodes/gstd/src/platform_application_io.rs b/node-graph/nodes/gstd/src/platform_application_io.rs index d2a85f7188e..e87eaa61d8c 100644 --- a/node-graph/nodes/gstd/src/platform_application_io.rs +++ b/node-graph/nodes/gstd/src/platform_application_io.rs @@ -173,7 +173,7 @@ fn string_to_bytes(_: impl Ctx, string: Item) -> Item { /// Converts extracted raw RGBA pixel data from an input image. Each pixel becomes 4 sequential bytes. Useful for transmission over HTTP or writing to files. #[node_macro::node(category("Data: I/O"), icon("NodeImageToBytes"), name("Image to Bytes"))] fn image_to_bytes(_: impl Ctx, image: Item>) -> Item { - let bytes: Vec = image + let bytes: Vec = raster_kernels::evaluate_raster_effects(image) .element() .data .iter() diff --git a/node-graph/nodes/raster/src/adjustments.rs b/node-graph/nodes/raster/src/adjustments.rs index 1f674ae3d09..60762659608 100644 --- a/node-graph/nodes/raster/src/adjustments.rs +++ b/node-graph/nodes/raster/src/adjustments.rs @@ -65,7 +65,7 @@ pub struct ImageChannels { /// Separates an image into its red, green, blue, and alpha channels, each provided as a grayscale image. #[node_macro::node(category("Raster"), icon("NodeImageToChannels"), name("Image to Channels"))] fn image_to_channels(_: impl Ctx, image: Item>) -> ImageChannels { - let (image, attributes) = image.into_parts(); + let (image, attributes) = raster_kernels::evaluate_raster_effects(image).into_parts(); let (width, height) = (image.width, image.height); // O(4 × pixels), since all four channels are written even when only some outputs are connected diff --git a/node-graph/nodes/raster/src/dehaze.rs b/node-graph/nodes/raster/src/dehaze.rs index 5ec82cc4e3f..7e41632d0d2 100644 --- a/node-graph/nodes/raster/src/dehaze.rs +++ b/node-graph/nodes/raster/src/dehaze.rs @@ -2,6 +2,7 @@ use core_types::context::Ctx; use core_types::list::Item; use image::{DynamicImage, GenericImage, GenericImageView, GrayImage, ImageBuffer, Luma, Rgba, RgbaImage}; use ndarray::{Array2, ArrayBase, Dim, OwnedRepr}; +use raster_kernels::evaluate_raster_effects; use raster_types::Image; use raster_types::{CPU, Raster}; use std::cmp::{max, min}; @@ -17,7 +18,7 @@ fn dehaze( ) -> Item> { let strength = *strength.element(); - let (image, attributes) = image_frame.into_parts(); + let (image, attributes) = evaluate_raster_effects(image_frame).into_parts(); let (width, height) = (image.width, image.height); // Prepare the image data for processing diff --git a/node-graph/nodes/raster/src/filter.rs b/node-graph/nodes/raster/src/filter.rs index 691ab3a5952..1ffeddd73fc 100644 --- a/node-graph/nodes/raster/src/filter.rs +++ b/node-graph/nodes/raster/src/filter.rs @@ -1,123 +1,12 @@ -use bytemuck::{Pod, Zeroable}; -use core_types::color::{Alpha, Color, Pixel, RGB}; +use core_types::color::Color; use core_types::context::Ctx; use core_types::list::Item; +use raster_kernels::evaluate_raster_effects; +use raster_types::Bitmap; use raster_types::Image; use raster_types::parallel_rows::fill_rows; -use raster_types::{Bitmap, BitmapMut}; use raster_types::{CPU, Raster}; -/// Working-buffer pixel for the blur algorithms' `gamma` mode: premultiplied sRGB-gamma `f32` channels. -/// Only used internally so the working buffer's color space is reflected in the type instead of stuffed into `Color` (which is linear-light by invariant). -#[repr(C)] -#[derive(Debug, Default, Clone, Copy, PartialEq, Pod, Zeroable)] -struct PremultipliedGammaPixel { - r: f32, - g: f32, - b: f32, - a: f32, -} - -impl Pixel for PremultipliedGammaPixel {} - -impl RGB for PremultipliedGammaPixel { - type ColorChannel = f32; - fn red(&self) -> f32 { - self.r - } - fn green(&self) -> f32 { - self.g - } - fn blue(&self) -> f32 { - self.b - } -} - -impl Alpha for PremultipliedGammaPixel { - type AlphaChannel = f32; - const TRANSPARENT: Self = Self { r: 0., g: 0., b: 0., a: 0. }; - fn alpha(&self) -> f32 { - self.a - } - fn multiplied_alpha(&self, mult: f32) -> Self { - Self { - r: self.r * mult, - g: self.g * mult, - b: self.b * mult, - a: self.a * mult, - } - } -} - -fn premultiply_gamma(buffer: Image) -> Image { - Image { - width: buffer.width, - height: buffer.height, - data: buffer - .data - .into_iter() - .map(|px| { - let [r, g, b, a] = px.to_gamma_srgb_channels(); - PremultipliedGammaPixel { r: r * a, g: g * a, b: b * a, a } - }) - .collect(), - } -} - -fn unpremultiply_gamma_to_linear(buffer: Image) -> Image { - Image { - width: buffer.width, - height: buffer.height, - data: buffer - .data - .into_iter() - .map(|px| { - if px.a > 0. { - let inv_a = 1. / px.a; - Color::from_gamma_srgb_channels(px.r * inv_a, px.g * inv_a, px.b * inv_a, px.a) - } else { - Color::TRANSPARENT - } - }) - .collect(), - } -} - -/// Blurs the image with a Gaussian or box blur kernel filter. -#[node_macro::node(category("Raster: Filter"), icon("NodeBlur"))] -fn blur( - _: impl Ctx, - /// The image to be blurred. - image_frame: Item>, - /// The radius of the blur kernel. - #[unit(" px")] - #[range] - #[hard(0..)] - #[soft(..100)] - radius: Item, - /// Use a lower-quality box kernel instead of a circular Gaussian kernel. This is faster but produces boxy artifacts. - box_blur: Item, - /// Opt to incorrectly apply the filter with color calculations in gamma space for compatibility with the results from other software. - gamma: Item, -) -> Item> { - let (radius, box_blur, gamma) = (*radius.element(), *box_blur.element(), *gamma.element()); - - let (image, attributes) = image_frame.into_parts(); - - // TODO: Scale the radius through the footprint so it acts as the document-space length its Pixels unit promises, with a - // window that varies continuously rather than snapping to whole source pixels - let blurred_image = if radius < 0.1 { - // Minimum blur radius - image - } else if box_blur { - Raster::new_cpu(box_blur_algorithm(image.into_data(), radius, gamma)) - } else { - Raster::new_cpu(gaussian_blur_algorithm(image.into_data(), radius, gamma)) - }; - - Item::from_parts(blurred_image, attributes) -} - /// Applies a median filter to reduce noise while preserving edges. #[node_macro::node(category("Raster: Filter"), icon("NodeMedianFilter"), memoize)] fn median_filter( @@ -133,7 +22,7 @@ fn median_filter( ) -> Item> { let radius = *radius.element(); - let (image, attributes) = image_frame.into_parts(); + let (image, attributes) = evaluate_raster_effects(image_frame).into_parts(); // TODO: Scale the radius through the footprint so it acts as the document-space length its Pixels unit promises, with a // window that varies continuously rather than snapping to whole source pixels @@ -147,156 +36,6 @@ fn median_filter( Item::from_parts(filtered_image, attributes) } -// 1D gaussian kernel -fn gaussian_kernel(radius: f64, image_extent: u32) -> Vec { - // Given radius, compute the size of the kernel that's approximately three times the radius - // Taps further out than the image extends never land on a pixel, so the kernel stops there while its weights keep the full radius - let kernel_radius = ((3. * radius).ceil() as usize).min(image_extent as usize); - let kernel_size = 2 * kernel_radius + 1; - let mut gaussian_kernel: Vec = vec![0.; kernel_size]; - - // Kernel values - let two_radius_squared = 2. * radius * radius; - let sum = gaussian_kernel - .iter_mut() - .enumerate() - .map(|(i, value_at_index)| { - let x = i as f64 - kernel_radius as f64; - let exponent = -(x * x) / two_radius_squared; - *value_at_index = exponent.exp(); - *value_at_index - }) - .sum::(); - - // Normalize - gaussian_kernel.iter_mut().for_each(|value_at_index| *value_at_index /= sum); - - gaussian_kernel -} - -fn gaussian_blur_algorithm(buffer: Image, radius: f64, gamma: bool) -> Image { - let kernel = gaussian_kernel(radius, buffer.width.max(buffer.height)); - if gamma { - let working = premultiply_gamma(buffer); - let blurred = gaussian_separable(working, &kernel, |r, g, b, a| PremultipliedGammaPixel { r, g, b, a }); - unpremultiply_gamma_to_linear(blurred) - } else { - let mut working = buffer; - working.map_pixels(|px| px.to_associated_alpha()); - let mut blurred = gaussian_separable(working, &kernel, Color::from_rgbaf32_unchecked); - blurred.map_pixels(|px| px.to_unassociated_alpha()); - blurred - } -} - -fn box_blur_algorithm(buffer: Image, radius: f64, gamma: bool) -> Image { - if gamma { - let working = premultiply_gamma(buffer); - let blurred = box_separable(working, radius, |r, g, b, a| PremultipliedGammaPixel { r, g, b, a }); - unpremultiply_gamma_to_linear(blurred) - } else { - let mut working = buffer; - working.map_pixels(|px| px.to_associated_alpha()); - let mut blurred = box_separable(working, radius, Color::from_rgbaf32_unchecked); - blurred.map_pixels(|px| px.to_unassociated_alpha()); - blurred - } -} - -fn gaussian_separable(buffer: Image

, kernel: &[f64], construct: F) -> Image

-where - P: Pixel + Copy + RGB + Alpha, - F: Fn(f32, f32, f32, f32) -> P, -{ - let (width, height) = buffer.dimensions(); - let half_kernel = kernel.len() / 2; - - let mut x_axis = Image::new(width, height, P::default()); - let mut y_axis = Image::new(width, height, P::default()); - - for pass in [false, true] { - let (max, old_buffer, current_buffer) = match pass { - false => (width, &buffer, &mut x_axis), - true => (height, &x_axis, &mut y_axis), - }; - let pass = pass as usize; - - for y in 0..height { - for x in 0..width { - let (mut r_sum, mut g_sum, mut b_sum, mut a_sum, mut weight_sum) = (0., 0., 0., 0., 0.); - - for (i, &weight) in kernel.iter().enumerate() { - let p = [x, y][pass] as i32 + (i as i32 - half_kernel as i32); - - if p >= 0 - && p < max as i32 - && let Some(px) = old_buffer.get_pixel([p as u32, x][pass], [y, p as u32][pass]) - { - r_sum += px.r() as f64 * weight; - g_sum += px.g() as f64 * weight; - b_sum += px.b() as f64 * weight; - a_sum += px.a() as f64 * weight; - weight_sum += weight; - } - } - - let (r, g, b, a) = if weight_sum > 0. { - ((r_sum / weight_sum) as f32, (g_sum / weight_sum) as f32, (b_sum / weight_sum) as f32, (a_sum / weight_sum) as f32) - } else { - let px = old_buffer.get_pixel(x, y).unwrap(); - (px.r(), px.g(), px.b(), px.a()) - }; - current_buffer.set_pixel(x, y, construct(r, g, b, a)); - } - } - } - - y_axis -} - -fn box_separable(buffer: Image

, radius: f64, construct: F) -> Image

-where - P: Pixel + Copy + RGB + Alpha, - F: Fn(f32, f32, f32, f32) -> P, -{ - let (width, height) = buffer.dimensions(); - let mut x_axis = Image::new(width, height, P::default()); - let mut y_axis = Image::new(width, height, P::default()); - - let radius = radius.min(width.max(height) as f64) as i32; - - for pass in [false, true] { - let (max, old_buffer, current_buffer) = match pass { - false => (width, &buffer, &mut x_axis), - true => (height, &x_axis, &mut y_axis), - }; - let pass = pass as usize; - - for y in 0..height { - for x in 0..width { - let (mut r_sum, mut g_sum, mut b_sum, mut a_sum, mut weight_sum) = (0., 0., 0., 0., 0.); - - let i = [x, y][pass]; - for d in (i as i32 - radius).max(0)..=(i as i32 + radius).min(max as i32 - 1) { - if let Some(px) = old_buffer.get_pixel([d as u32, x][pass], [y, d as u32][pass]) { - let weight = 1.; - r_sum += px.r() as f64 * weight; - g_sum += px.g() as f64 * weight; - b_sum += px.b() as f64 * weight; - a_sum += px.a() as f64 * weight; - weight_sum += weight; - } - } - - let (r, g, b, a) = ((r_sum / weight_sum) as f32, (g_sum / weight_sum) as f32, (b_sum / weight_sum) as f32, (a_sum / weight_sum) as f32); - current_buffer.set_pixel(x, y, construct(r, g, b, a)); - } - } - } - - y_axis -} - fn median_filter_algorithm(original_buffer: Image, radius: u32) -> Image { let (width, height) = original_buffer.dimensions(); let mut output = Image::new(width, height, Color::TRANSPARENT); diff --git a/node-graph/nodes/raster/src/image_color_palette.rs b/node-graph/nodes/raster/src/image_color_palette.rs index 30b4e271bff..c6d6ff68b02 100644 --- a/node-graph/nodes/raster/src/image_color_palette.rs +++ b/node-graph/nodes/raster/src/image_color_palette.rs @@ -2,6 +2,7 @@ use color::{ColorSpace, Oklab}; use core_types::color::Color; use core_types::context::Ctx; use core_types::list::{Item, List}; +use raster_kernels::evaluate_raster_effects; use raster_types::{CPU, Raster}; use std::collections::BinaryHeap; use std::ops::{AddAssign, SubAssign}; @@ -25,6 +26,7 @@ fn image_color_palette( count: Item, ) -> List { let count = *count.element() as usize; + let image = evaluate_raster_effects(image); // O(n) binning of the image's n pixels into an OkLab histogram, weighted by alpha so transparent pixels are ignored let mut cumulative = vec![Moments::default(); TABLE_SIDE.pow(3)]; diff --git a/node-graph/nodes/raster/src/std_nodes.rs b/node-graph/nodes/raster/src/std_nodes.rs index 7504ee1ea4b..c7610f03f66 100644 --- a/node-graph/nodes/raster/src/std_nodes.rs +++ b/node-graph/nodes/raster/src/std_nodes.rs @@ -1,7 +1,7 @@ use crate::adjustments::{CellularDistanceFunction, CellularReturnType, DomainWarpType, FractalType, NoiseType}; use core_types::ATTR_TRANSFORM; use core_types::color::Color; -use core_types::color::{Alpha, AlphaMut, Channel, LinearChannel, Luminance, RGBMut}; +use core_types::color::{Alpha, AlphaMut, LinearChannel, Luminance, RGBMut}; use core_types::context::{Ctx, ExtractFootprint}; use core_types::list::Item; use core_types::math::bbox::Bbox; @@ -10,6 +10,7 @@ use dyn_any::DynAny; use glam::{DAffine2, DVec2, UVec2}; use graphene_resource::Resource; use graphic_types::generator::{GeneratorPattern, NoiseSettings}; +use raster_kernels::evaluate_raster_effects; use raster_kernels::generators::render_pattern; use raster_types::parallel_rows::fill_rows; use raster_types::{Bitmap, BitmapMut}; @@ -51,51 +52,68 @@ fn channels_to_image( ) -> Result>, SizeMismatchError> { // An unconnected channel arrives as the default zero-sized raster, which counts as absent let present = |channel: Item>| (channel.element().width > 0 && channel.element().height > 0).then_some(channel); - let (red, green, blue, alpha) = (present(red), present(green), present(blue), present(alpha)); - - // Take this item's transform and blending attributes from the first present channel - let Some(attributes) = [&red, &green, &blue, &alpha].iter().find_map(|channel| channel.as_ref()).map(|channel| channel.attributes().clone()) else { - return Ok(Item::default()); - }; + let channels = [red, green, blue, alpha].map(present); // All present channels must share the same dimensions - let channel_dimensions = [("Red", &red), ("Green", &green), ("Blue", &blue), ("Alpha", &alpha)] - .map(|(name, channel)| channel.as_ref().map(|channel| (name, UVec2::new(channel.element().width, channel.element().height)))); - let Some(&(first, first_size)) = channel_dimensions.iter().flatten().next() else { + let dimensions = |channel: &Item>| UVec2::new(channel.element().width, channel.element().height); + let mut present_channels = ["Red", "Green", "Blue", "Alpha"] + .into_iter() + .zip(&channels) + .filter_map(|(name, channel)| Some((name, channel.as_ref()?))); + let Some((first, first_channel)) = present_channels.next() else { return Ok(Item::default()); }; - if let Some(&(second, second_size)) = channel_dimensions.iter().flatten().find(|&&(_, size)| size != first_size) { + let (first_size, transform) = (dimensions(first_channel), first_channel.attribute_cloned_or_default::(ATTR_TRANSFORM)); + if let Some((second, second_channel)) = present_channels.find(|(_, channel)| dimensions(channel) != first_size) { return Err(SizeMismatchError { first, first_size, second, - second_size, + second_size: dimensions(second_channel), }); } let (width, height) = (first_size.x, first_size.y); + // A channel's effects grow it by the margin they cast past its edges, so every channel is placed within the widest margin + let channels = channels.map(|channel| channel.map(evaluate_raster_effects)); + let margin = |channel: &Item>| ((channel.element().width - width) / 2, (channel.element().height - height) / 2); + let (margin_x, margin_y) = channels + .iter() + .flatten() + .map(margin) + .fold((0, 0), |(x, y), (channel_x, channel_y)| (x.max(channel_x), y.max(channel_y))); + + // Take this item's blending attributes from the first present channel, whose effects are carried out by now + let Some(mut attributes) = channels.iter().flatten().next().map(|channel| channel.attributes().clone()) else { + return Ok(Item::default()); + }; + let size = DVec2::new(width as f64, height as f64); + let unit_margin = DVec2::new(margin_x as f64, margin_y as f64) / size; + attributes.insert( + ATTR_TRANSFORM, + transform * DAffine2::from_translation(-unit_margin) * DAffine2::from_scale(DVec2::ONE + 2. * unit_margin), + ); + + // A channel's value at a pixel of the output, with nothing past its own edges, or `None` if the channel is absent + let value = |channel: &Option>>, x: u32, y: u32| { + channel.as_ref().map(|channel| { + let (channel_margin_x, channel_margin_y) = margin(channel); + let (x, y) = ((x + channel_margin_x).checked_sub(margin_x), (y + channel_margin_y).checked_sub(margin_y)); + x.zip(y).and_then(|(x, y)| channel.element().get_pixel(x, y)).map_or(0., |pixel| pixel.l().cast_linear_channel()) + }) + }; + // Set each output pixel's channels from the present inputs, defaulting absent color channels to 0 and absent alpha to 1 - let mut image = Image::new(width, height, Color::TRANSPARENT); + let [red, green, blue, alpha] = &channels; + let mut image = Image::new(width + 2 * margin_x, height + 2 * margin_y, Color::TRANSPARENT); for y in 0..image.height() { for x in 0..image.width() { let image_pixel = image.get_pixel_mut(x, y).unwrap(); - match red.as_ref().and_then(|r| r.element().get_pixel(x, y)) { - Some(r) => image_pixel.set_red(r.l().cast_linear_channel()), - None => image_pixel.set_red(Channel::from_linear(0.)), - } - match green.as_ref().and_then(|g| g.element().get_pixel(x, y)) { - Some(g) => image_pixel.set_green(g.l().cast_linear_channel()), - None => image_pixel.set_green(Channel::from_linear(0.)), - } - match blue.as_ref().and_then(|b| b.element().get_pixel(x, y)) { - Some(b) => image_pixel.set_blue(b.l().cast_linear_channel()), - None => image_pixel.set_blue(Channel::from_linear(0.)), - } - match alpha.as_ref().and_then(|a| a.element().get_pixel(x, y)) { - Some(a) => image_pixel.set_alpha(a.l().cast_linear_channel()), - None => image_pixel.set_alpha(Channel::from_linear(1.)), - } + image_pixel.set_red(value(red, x, y).unwrap_or(0.)); + image_pixel.set_green(value(green, x, y).unwrap_or(0.)); + image_pixel.set_blue(value(blue, x, y).unwrap_or(0.)); + image_pixel.set_alpha(value(alpha, x, y).unwrap_or(1.)); } } @@ -111,6 +129,9 @@ fn mask( #[expose] stencil: Item>, ) -> Item> { + let image = evaluate_raster_effects(image); + let stencil = evaluate_raster_effects(stencil); + // An absent stencil arrives as the default empty raster, leaving the image unmasked if stencil.element().width == 0 || stencil.element().height == 0 { return image; @@ -150,6 +171,7 @@ fn mask( #[node_macro::node(category(""))] fn extend_image_to_bounds(_: impl Ctx, image: Item>, bounds: Item) -> Result>, (Item>, ImageError)> { + let image = evaluate_raster_effects(image); let bounds = *bounds.element(); let image_transform: DAffine2 = image.attribute_cloned_or_default(ATTR_TRANSFORM); @@ -341,3 +363,39 @@ fn mandelbrot(ctx: impl ExtractFootprint + Send) -> Result>, Im Ok(Item::new_from_element(Raster::new_cpu(image)).with_attribute(ATTR_TRANSFORM, DAffine2::from_translation(offset) * DAffine2::from_scale(size))) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn channels_stay_aligned_when_effects_grow_only_one() { + use graphic_types::{Effect, EffectOperation, stamp_effect}; + + let gray = |value: f32| { + let image = Image::new(8, 8, Color::from_rgbaf32_unchecked(value, value, value, 1.)); + Item::new_from_element(Raster::new_cpu(image)).with_attribute(ATTR_TRANSFORM, DAffine2::from_scale(DVec2::splat(8.))) + }; + + // Only the red channel is blurred, which grows it by the blur's margin + let mut red = gray(1.); + stamp_effect( + &mut red, + Effect::new(&EffectOperation::Blur { + radius: 1., + box_blur: false, + gamma: false, + }), + ); + let image = channels_to_image((), (), red, gray(0.5), gray(0.25), gray(1.)).unwrap(); + + // The other channels are placed within the same margin, and the whole image keeps its place in the document + let raster = image.element(); + assert!(raster.width > 8 && raster.height > 8, "the image grows to hold the blur, rather than coming out empty"); + let transform: DAffine2 = image.attribute_cloned_or_default(ATTR_TRANSFORM); + let to_pixels = DAffine2::from_scale(DVec2::new(raster.width as f64, raster.height as f64)) * transform.inverse(); + let center = to_pixels.transform_point2(DVec2::splat(4.)).as_uvec2(); + let pixel = raster.get_pixel(center.x, center.y).unwrap(); + assert!((pixel.g() - 0.5).abs() < 1e-6 && (pixel.b() - 0.25).abs() < 1e-6, "the unblurred channels line up: {pixel:?}"); + } +} diff --git a/node-graph/nodes/vectorize/src/lib.rs b/node-graph/nodes/vectorize/src/lib.rs index a0295fc4117..779bbd4a464 100644 --- a/node-graph/nodes/vectorize/src/lib.rs +++ b/node-graph/nodes/vectorize/src/lib.rs @@ -37,7 +37,7 @@ const BROWSER_MAX_MEGABYTES: f64 = 1000.; /// Traces an image into a stack of filled and stroked shapes, drawn bottom to top as an artist would layer them. /// /// The trace runs in slices, one per evaluation, showing the shapes traced so far until it is done. -#[node_macro::node(category("Raster"), memoize)] +#[node_macro::node(category("Raster"), icon("NodeVectorize"), memoize)] fn vectorize( _: impl Ctx, /// The image to trace. From f97078d2bcbc017350462b55bc91ef805a53ea04 Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Wed, 18 Nov 2026 02:22:51 -0800 Subject: [PATCH 2/2] Fix effects being clipped, misordered, or misscaled, and bound their cost --- Cargo.lock | 1 + .../network_interface/structure.rs | 1 + .../runtime/thumbnails/atlas.rs | 4 +- .../libraries/graphic-types/src/effects.rs | 75 +++++++++++- .../raster-kernels/src/effect_kernels.rs | 12 ++ .../raster-kernels/src/raster_effects.rs | 73 +++++++++-- .../libraries/rendering/src/renderer.rs | 113 ++++++++++++++---- .../rendering/src/vello_scene_builder.rs | 16 ++- .../wgpu-executor/src/vello_effects.rs | 3 + .../wgpu-executor/src/vello_render.rs | 17 +-- node-graph/nodes/raster/src/adjust.rs | 20 +++- node-graph/nodes/raster/src/adjustments.rs | 31 ++++- node-graph/nodes/raster/src/blending_nodes.rs | 6 +- node-graph/nodes/vectorize/Cargo.toml | 1 + node-graph/nodes/vectorize/src/lib.rs | 18 +-- node-graph/nodes/vectorize/src/slices.rs | 20 +++- 16 files changed, 344 insertions(+), 67 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ab342de6a0d..7c48feee489 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8043,6 +8043,7 @@ dependencies = [ "log", "node-macro", "ollev", + "raster-kernels", "raster-types", "vector-types", ] diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/structure.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/structure.rs index 286e820885d..81e35d845d5 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/structure.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/structure.rs @@ -203,6 +203,7 @@ impl NodeNetworkInterface { /// Update how far each layer's effects paint past its content. pub fn update_effects_reach(&mut self, new_layer_effects_reach: HashMap) { + self.document_metadata.mark_changed(); self.document_metadata.layer_effects_reach = new_layer_effects_reach; } } diff --git a/editor/src/node_graph_executor/runtime/thumbnails/atlas.rs b/editor/src/node_graph_executor/runtime/thumbnails/atlas.rs index ede1e2aeffb..a3c7a2a75d5 100644 --- a/editor/src/node_graph_executor/runtime/thumbnails/atlas.rs +++ b/editor/src/node_graph_executor/runtime/thumbnails/atlas.rs @@ -78,7 +78,9 @@ pub fn draw_atlas(scene: &mut VelloSceneBuilder, layout: &ThumbnailAtlasLayout, // The clip also limits content filling the whole scene, like a background, to the cell let cell = Rect::new(origin.x, origin.y, origin.x + cell_size.x, origin.y + cell_size.y); scene.push_clip_layer(Fill::NonZero, Affine::IDENTITY, &cell); - content.graphic().render_to_vello(scene, transform, &render_params); + scene.draw_placed(Affine::new(transform.to_cols_array()), |scene| { + content.graphic().render_to_vello(scene, DAffine2::IDENTITY, &render_params) + }); scene.pop_layer(); } } diff --git a/node-graph/libraries/graphic-types/src/effects.rs b/node-graph/libraries/graphic-types/src/effects.rs index 5090b1b8ff3..d590f4719a3 100644 --- a/node-graph/libraries/graphic-types/src/effects.rs +++ b/node-graph/libraries/graphic-types/src/effects.rs @@ -6,7 +6,7 @@ use core_types::Color; use core_types::color::SRGBA8; use core_types::graphene_hash::CacheHash; use core_types::list::{ATTR_EFFECTS, Item, List}; -use glam::DVec2; +use glam::{DMat2, DVec2}; use std::fmt::Debug; /// The image operation an [`Effect`] carries out. @@ -52,11 +52,11 @@ impl Effect { *self.0.element() } - /// Decodes the parameters into an [`EffectOperation`], falling back to the default for any absent attribute. + /// Decodes the parameters into an [`EffectOperation`], falling back to the default for any absent attribute and to zero for any length that isn't finite. pub fn operation(&self) -> EffectOperation { let mut operation = EffectOperation::placeholder(self.kind()); operation.visit_parameters(&mut ReadParameters(&self.0)); - operation + operation.with_finite_lengths() } } @@ -84,6 +84,14 @@ impl Effects { self.effects().map(Effect::operation) } + /// Whether carrying out the effects leaves the content as it is, like a blur of no radius, so renderers can skip them. + pub fn change_nothing(&self) -> bool { + self.operations().all(|operation| match operation { + EffectOperation::Blur { radius, .. } => radius <= 0., + EffectOperation::DropShadow { color, .. } | EffectOperation::InnerShadow { color, .. } | EffectOperation::OuterGlow { color, .. } => color.a() <= 0., + }) + } + /// How far past the content the effects together can paint, in document pixels. pub fn reach(&self) -> f64 { self.operations().map(|operation| operation.reach()).sum() @@ -98,6 +106,11 @@ impl Effects { } } +/// The most that a length in the document spans along each axis of a space, given the linear map from the document into that space. +pub fn spans_along_axes(document_to_space: DMat2) -> DVec2 { + DVec2::new(document_to_space.row(0).length(), document_to_space.row(1).length()) +} + /// Appends an effect to the item's list, after those already applied to it. pub fn stamp_effect(item: &mut Item, effect: Effect) { item.attribute_mut_or_insert_default::(ATTR_EFFECTS).0.push(Item::new_from_element(effect)); @@ -153,12 +166,43 @@ impl EffectOperation { // A Gaussian is negligible past three standard deviations match self { Self::Blur { radius, .. } => 3. * radius.max(0.), - Self::DropShadow { offset, blur, spread, .. } => offset.length() + spread.max(0.) + 3. * blur.max(0.), - Self::InnerShadow { offset, blur, .. } => offset.length() + 3. * blur.max(0.), + // An inner shadow paints within the content but reads this far out to find the edges it casts from + Self::DropShadow { offset, blur, spread, .. } | Self::InnerShadow { offset, blur, spread, .. } => offset.length() + spread.max(0.) + 3. * blur.max(0.), Self::OuterGlow { blur, spread, .. } => spread.max(0.) + 3. * blur.max(0.), } } + /// This operation with each length that isn't finite (like one computed by dividing by zero) made zero, since no renderer can carry it out. + fn with_finite_lengths(self) -> Self { + let finite = |length: f64| if length.is_finite() { length } else { 0. }; + let finite_offset = |offset: DVec2| if offset.is_finite() { offset } else { DVec2::ZERO }; + + match self { + Self::Blur { radius, box_blur, gamma } => Self::Blur { + radius: finite(radius), + box_blur, + gamma, + }, + Self::DropShadow { color, offset, blur, spread } => Self::DropShadow { + color, + offset: finite_offset(offset), + blur: finite(blur), + spread: finite(spread), + }, + Self::InnerShadow { color, offset, blur, spread } => Self::InnerShadow { + color, + offset: finite_offset(offset), + blur: finite(blur), + spread: finite(spread), + }, + Self::OuterGlow { color, blur, spread } => Self::OuterGlow { + color, + blur: finite(blur), + spread: finite(spread), + }, + } + } + /// An operation of this kind with arbitrary parameters, for a visitor to fill in. fn placeholder(kind: EffectKind) -> Self { match kind { @@ -272,4 +316,25 @@ mod tests { let default_row = Effect::new(&EffectOperation::default_for(EffectKind::DropShadow)); assert_eq!(default_row.0.attributes().iter_any().count(), 0, "a default operation stamps no parameters"); } + + #[test] + fn lengths_that_arent_finite_decode_as_zero() { + let row = Effect::new(&EffectOperation::DropShadow { + color: Color::BLACK, + offset: DVec2::new(f64::NAN, 1.), + blur: f64::INFINITY, + spread: 3., + }); + + let EffectOperation::DropShadow { offset, blur, spread, .. } = row.operation() else { + panic!("the row changed kind") + }; + assert_eq!((offset, blur, spread), (DVec2::ZERO, 0., 3.)); + } + + #[test] + fn only_effects_that_show_change_something() { + assert!(Effects::from_operations([EffectOperation::default_for(EffectKind::Blur)]).change_nothing()); + assert!(!Effects::from_operations([EffectOperation::default_for(EffectKind::DropShadow)]).change_nothing()); + } } diff --git a/node-graph/libraries/raster-kernels/src/effect_kernels.rs b/node-graph/libraries/raster-kernels/src/effect_kernels.rs index 1e5e2fcef15..b7ecabfdd16 100644 --- a/node-graph/libraries/raster-kernels/src/effect_kernels.rs +++ b/node-graph/libraries/raster-kernels/src/effect_kernels.rs @@ -182,6 +182,8 @@ impl Plane { pub fn dilated(self, horizontal: f64, vertical: f64) -> Self { self.along_both_axes(horizontal, vertical, |mut plane, distance| { let length = plane.width; + // Growing past the row's whole length changes nothing more + let distance = distance.min(length as f64); // Steps that double, then the remainder, so each step grows the shapes by up to as much as all the steps before it let (mut covered, mut step) = (0., 1.); @@ -331,6 +333,16 @@ mod tests { assert_eq!(covered, 7 * 7, "a point grows into a 7 by 7 square"); } + #[test] + fn dilation_by_an_infinite_distance_covers_the_image() { + let mut plane = Plane::::new(21, 21); + plane.data[0] = 1.; + + let dilated = plane.dilated(f64::INFINITY, f64::INFINITY); + + assert!(dilated.data.iter().all(|&value| value >= 0.999)); + } + #[test] fn shifting_moves_by_fractions_of_a_pixel() { let mut plane = Plane::::new(4, 1); diff --git a/node-graph/libraries/raster-kernels/src/raster_effects.rs b/node-graph/libraries/raster-kernels/src/raster_effects.rs index 032d54ed5ee..a86055afb86 100644 --- a/node-graph/libraries/raster-kernels/src/raster_effects.rs +++ b/node-graph/libraries/raster-kernels/src/raster_effects.rs @@ -6,7 +6,7 @@ use core_types::color::{linear_to_srgb, srgb_to_linear}; use core_types::list::{ATTR_EFFECTS, Item}; use core_types::{ATTR_OPACITY_FILL, ATTR_TRANSFORM, Color}; use glam::{DAffine2, DMat2, DVec2}; -use graphic_types::effects::{EffectOperation, Effects}; +use graphic_types::effects::{EffectOperation, Effects, spans_along_axes}; use raster_types::{CPU, Image, Raster}; /// The most pixels an image may grow to with the margin its effects cast into, beyond which the margin is cut short. @@ -37,7 +37,7 @@ pub fn evaluate_raster_effects(item: Item>) -> Item> { /// `None` when there's nothing to carry out, or the image is empty or its transform can't be inverted. fn evaluate(raster: &Raster, transform: DAffine2, effects: &Effects, fill: f32) -> Option<(Image, DAffine2)> { let (width, height) = (raster.width as usize, raster.height as usize); - if (effects.is_empty() && fill >= 1.) || width == 0 || height == 0 || transform.matrix2.determinant() == 0. { + if (effects.change_nothing() && fill >= 1.) || width == 0 || height == 0 || transform.matrix2.determinant() == 0. { return None; } let size = DVec2::new(width as f64, height as f64); @@ -45,14 +45,12 @@ fn evaluate(raster: &Raster, transform: DAffine2, effects: &Effects, fill: // Document lengths become pixels along each of the image's axes, and offsets map through the inverse of its pixel placement let pixels_to_document = transform.matrix2 * DMat2::from_diagonal(size.recip()); let document_to_pixels = pixels_to_document.inverse(); - let pixels_per_length = DVec2::new(pixels_to_document.x_axis.length(), pixels_to_document.y_axis.length()).recip(); + let pixels_per_length = spans_along_axes(document_to_pixels); - // A margin around the image to hold the blur and shadows cast past its edges, cut short for an image that would grow too large + // A margin around the image to hold the blur and shadows cast past its edges, cut short evenly for an image that would grow too large let mut margin = (effects.reach() * pixels_per_length).ceil(); - let grown_size = size + 2. * margin; - if grown_size.x * grown_size.y > MAX_PIXELS_WITH_MARGIN { - let shrunk_size = grown_size * (MAX_PIXELS_WITH_MARGIN / (grown_size.x * grown_size.y)).sqrt(); - margin = ((shrunk_size - size) / 2.).max(DVec2::ZERO).floor(); + if (size + 2. * margin).element_product() > MAX_PIXELS_WITH_MARGIN { + margin = (margin * margin_fraction_that_fits(size, margin)).floor(); } let (margin_x, margin_y) = (margin.x as usize, margin.y as usize); @@ -124,6 +122,23 @@ fn evaluate(raster: &Raster, transform: DAffine2, effects: &Effects, fill: Some((image, grown_transform)) } +/// The fraction of `margin` that grows an image of `size` to [`MAX_PIXELS_WITH_MARGIN`], or 0 if the image alone already reaches it. +fn margin_fraction_that_fits(size: DVec2, margin: DVec2) -> f64 { + // The positive root of (width + 2 × fraction × margin width) × (height + 2 × fraction × margin height) = the limit + let quadratic = 4. * margin.x * margin.y; + let linear = 2. * (size.x * margin.y + size.y * margin.x); + let constant = size.element_product() - MAX_PIXELS_WITH_MARGIN; + if constant >= 0. || linear <= 0. { + return 0.; + } + + let fraction = match quadratic > 0. { + true => (-linear + (linear * linear - 4. * quadratic * constant).sqrt()) / (2. * quadratic), + false => -constant / linear, + }; + fraction.clamp(0., 1.) +} + /// A color as the premultiplied, gamma-encoded channels the effects work with. fn premultiplied_gamma_channels(color: Color) -> [f32; 4] { let [red, green, blue, alpha] = color.to_gamma_srgb_channels(); @@ -200,3 +215,45 @@ fn composite_beneath(image: &Plane<[f32; 4]>, beneath: &Plane<[f32; 4]>, full_co let knocked_out = beneath.zip(full_content, |beneath, content| beneath.map(|channel| channel * (1. - content[3]))); image.zip(&knocked_out, |image, beneath| std::array::from_fn(|channel| image[channel] + beneath[channel])) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_margin_too_large_is_cut_short_to_fit_the_pixel_limit() { + // A tall strip whose margins alone would grow it eightfold past the limit + let size = DVec2::new(1., 16_384.); + let margin = DVec2::splat(3000.); + + let fitted = (margin * margin_fraction_that_fits(size, margin)).floor(); + let grown_area = (size + 2. * fitted).element_product(); + + assert!(grown_area <= MAX_PIXELS_WITH_MARGIN, "the grown image has {grown_area} pixels"); + assert!(grown_area > 0.99 * MAX_PIXELS_WITH_MARGIN, "the grown image has only {grown_area} pixels"); + } + + #[test] + fn an_inner_shadow_of_only_spread_shades_an_opaque_raster() { + let raster = Raster::new_cpu(Image::new(8, 8, Color::WHITE)); + let item = Item::new_from_element(raster).with_attribute(ATTR_TRANSFORM, DAffine2::from_scale(DVec2::splat(8.))).with_attribute( + ATTR_EFFECTS, + Effects::from_operations([EffectOperation::InnerShadow { + color: Color::BLACK, + offset: DVec2::ZERO, + blur: 0., + spread: 2., + }]), + ); + + let evaluated = evaluate_raster_effects(item); + let raster = evaluated.element(); + let width = raster.width as usize; + let pixel = |x: usize, y: usize| raster.data[y * width + x]; + + // The margin around the content lies outside it, which the shadow grows inward from + let margin = (width - 8) / 2; + assert!(pixel(margin, margin + 4).r() < 0.01, "the content's edge is shaded"); + assert!(pixel(margin + 4, margin + 4).r() > 0.99, "the content's middle is not"); + } +} diff --git a/node-graph/libraries/rendering/src/renderer.rs b/node-graph/libraries/rendering/src/renderer.rs index c224aa871cf..e4405f1ccd8 100644 --- a/node-graph/libraries/rendering/src/renderer.rs +++ b/node-graph/libraries/rendering/src/renderer.rs @@ -23,6 +23,7 @@ use core_types::{ use dyn_any::DynAny; use glam::{DAffine2, DMat2, DVec2}; use graphene_resource::Resource; +use graphic_types::effects::spans_along_axes; use graphic_types::raster_types::{CPU, GPU, Raster}; use graphic_types::vector_types::gradient::{Gradient, GradientForm}; use graphic_types::vector_types::vector::click_target::{ClickTarget, FreePoint, TargetPath}; @@ -108,7 +109,7 @@ impl<'a, T> ItemRef<'a, T> { // Outline mode shows only geometry let effects = self .attribute::(ATTR_EFFECTS) - .filter(|effects| !effects.is_empty() && render_params.render_mode != RenderMode::Outline); + .filter(|effects| !effects.change_nothing() && render_params.render_mode != RenderMode::Outline); match effects { Some(effects) => ItemCompositing { @@ -324,7 +325,9 @@ fn write_svg_effects_filter(svg_defs: &mut String, effects: &Effects, fill: f32, return None; } let document_to_user = user_to_document.matrix2.inverse(); - let length_scale = determinant.abs().sqrt().recip(); + let length_scale = spans_along_axes(document_to_user); + // SVG takes a length along each axis as a pair of numbers + let pair = |length: DVec2| format!("{} {}", length.x, length.y); let mut primitives = SvgFilterPrimitives::default(); @@ -338,7 +341,7 @@ fn write_svg_effects_filter(svg_defs: &mut String, effects: &Effects, fill: f32, let (color, offset, blur, spread, inner) = match effect { EffectOperation::Blur { radius, box_blur, gamma } => { let radius = radius * length_scale; - if radius < 0.01 { + if radius.max_element() < 0.01 { continue; } @@ -349,7 +352,7 @@ fn write_svg_effects_filter(svg_defs: &mut String, effects: &Effects, fill: f32, } // SVG has no box blur, so a box becomes the Gaussian of the same variance - let sigma = if box_blur { (radius * (radius + 1.) / 3.).sqrt() } else { radius }; + let sigma = pair(if box_blur { (radius * (radius + 1.) / 3.).map(f64::sqrt) } else { radius }); let space = if gamma { "sRGB" } else { "linearRGB" }; let attributes = |input: &str| format!(r#"in="{input}" stdDeviation="{sigma}" color-interpolation-filters="{space}""#); @@ -372,11 +375,12 @@ fn write_svg_effects_filter(svg_defs: &mut String, effects: &Effects, fill: f32, let opaque = primitives.push("feFlood", r#"flood-opacity="1""#, ""); coverage = primitives.push("feComposite", &format!(r#"in="{opaque}" in2="{coverage}" operator="out""#), ""); } - if spread * length_scale >= 0.01 { - coverage = primitives.push("feMorphology", &format!(r#"in="{coverage}" operator="dilate" radius="{}""#, spread * length_scale), ""); + let (spread, blur) = (spread * length_scale, blur * length_scale); + if spread.max_element() >= 0.01 { + coverage = primitives.push("feMorphology", &format!(r#"in="{coverage}" operator="dilate" radius="{}""#, pair(spread)), ""); } - if blur * length_scale >= 0.01 { - coverage = primitives.push("feGaussianBlur", &format!(r#"in="{coverage}" stdDeviation="{}""#, blur * length_scale), ""); + if blur.max_element() >= 0.01 { + coverage = primitives.push("feGaussianBlur", &format!(r#"in="{coverage}" stdDeviation="{}""#, pair(blur)), ""); } let flood = primitives.push( @@ -1122,10 +1126,10 @@ fn render_graphic_item_svg(item: ItemRef<'_, Graphic>, next_clips: bool, mask_st let render_item = |render: &mut SvgRender| { // The filter also covers how far the effects nested inside reach past the content, measured in this user space - let document_scale = render.transform.matrix2.determinant().abs().sqrt(); - let filter = compositing.svg_filter(render, || match element.bounding_box(transform, true) { + let document_to_user = render.transform.matrix2.inverse(); + let filter = compositing.svg_filter(render, || match drawn_bounding_box(element, transform) { RenderBoundingBox::Rectangle([min, max]) => { - let nested_reach = nested_effects_reach(element) / document_scale; + let nested_reach = nested_effects_reach(element) * spans_along_axes(document_to_user); RenderBoundingBox::Rectangle([min - nested_reach, max + nested_reach]) } bounds => bounds, @@ -1202,12 +1206,18 @@ fn render_graphic_item_to_vello<'a>( // The bounds are only worth finding for the layers or effects group that need them let bounds = if needs_layer || mask_element_and_transform.is_some() || compositing.effects.is_some() { - element.bounding_box(transform, true) + drawn_bounding_box(element, transform) } else { RenderBoundingBox::None }; + + // The layers and effects group also cover how far the effects nested inside reach past the content + let nested_reach = match bounds { + RenderBoundingBox::Rectangle(_) => nested_effects_reach(element), + _ => 0., + }; let layer_rect = match bounds { - RenderBoundingBox::Rectangle(bounds) => Some(inflate_bounds(bounds, compositing.reach())), + RenderBoundingBox::Rectangle(bounds) => Some(inflate_bounds(bounds, compositing.reach() + nested_reach)), _ => None, }; @@ -1224,9 +1234,8 @@ fn render_graphic_item_to_vello<'a>( false }; - // The effects group also covers how far the effects nested inside reach past the content let effects_bounds = || match bounds { - RenderBoundingBox::Rectangle(bounds) => rect_bounds(inflate_bounds(bounds, nested_effects_reach(element))), + RenderBoundingBox::Rectangle(bounds) => rect_bounds(inflate_bounds(bounds, nested_reach)), bounds => bounds, }; let draw_content = |scene: &mut VelloSceneBuilder| { @@ -3462,7 +3471,7 @@ fn text_item_size_and_transform(item: ItemRef<'_, String>) -> Option<(DVec2, DAf pub fn text_list_bounding_box(list: &List, outer_transform: DAffine2) -> RenderBoundingBox { let mut bounds: Option<[DVec2; 2]> = None; for index in 0..list.len() { - accumulate_text_item_bounds(ItemRef::ListItem(list, index), outer_transform, &mut bounds); + accumulate_text_item_bounds(ItemRef::ListItem(list, index), outer_transform, false, &mut bounds); } match bounds { Some(bounds) => RenderBoundingBox::Rectangle(bounds), @@ -3470,11 +3479,14 @@ pub fn text_list_bounding_box(list: &List, outer_transform: DAffine2) -> } } -/// Folds one laid-out text item's corner points into the running bounds. -fn accumulate_text_item_bounds(item: ItemRef<'_, String>, outer_transform: DAffine2, bounds: &mut Option<[DVec2; 2]>) { +/// Folds one laid-out text item's corner points into the running bounds, padded by its font size for glyphs overhanging their layout box if `padded`. +fn accumulate_text_item_bounds(item: ItemRef<'_, String>, outer_transform: DAffine2, padded: bool, bounds: &mut Option<[DVec2; 2]>) { let Some((size, transform)) = text_item_size_and_transform(item) else { return }; + let padding = if padded { item.attribute_cloned_or(ATTR_FONT_SIZE, DEFAULT_FONT_SIZE) } else { 0. }; + let (min, max) = (DVec2::splat(-padding), size + padding); + let full_transform = outer_transform * transform; - for corner in [DVec2::ZERO, DVec2::new(size.x, 0.), DVec2::new(0., size.y), size] { + for corner in [min, DVec2::new(max.x, min.y), DVec2::new(min.x, max.y), max] { let point = full_transform.transform_point2(corner); *bounds = Some(match *bounds { Some([min, max]) => [min.min(point), max.max(point)], @@ -3516,7 +3528,7 @@ fn graphic_thumbnail_bounding_box(graphic: &Graphic, item_transform: DAffine2) - match graphic { Graphic::Text(item) => { let mut bounds = None; - accumulate_text_item_bounds(ItemRef::Item(item), item_transform, &mut bounds); + accumulate_text_item_bounds(ItemRef::Item(item), item_transform, false, &mut bounds); match bounds { Some(bounds) => RenderBoundingBox::Rectangle(bounds), None => RenderBoundingBox::None, @@ -3533,6 +3545,37 @@ fn graphic_thumbnail_bounding_box(graphic: &Graphic, item_transform: DAffine2) - } } +/// The bounds a graphic draws within, like the `BoundingBox` trait's with strokes included, but laying out its text, which that reports as `None`. +/// O(items), with a layout for each text item. +fn drawn_bounding_box(graphic: &Graphic, transform: DAffine2) -> RenderBoundingBox { + let mut text_bounds = None; + match graphic { + Graphic::Text(item) => accumulate_text_item_bounds(ItemRef::Item(item), transform, true, &mut text_bounds), + Graphic::TextList(list) => (0..list.len()).for_each(|index| accumulate_text_item_bounds(ItemRef::ListItem(list, index), transform, true, &mut text_bounds)), + Graphic::Graphic(item) => return drawn_bounding_box(item.element(), transform * item.attribute_cloned_or_default::(ATTR_TRANSFORM)), + Graphic::GraphicList(list) => { + let mut combined = RenderBoundingBox::None; + for index in 0..list.len() { + let Some(element) = list.element(index) else { continue }; + let item_transform = transform * list.attribute_cloned_or_default::(ATTR_TRANSFORM, index); + + combined = match (combined, drawn_bounding_box(element, item_transform)) { + (RenderBoundingBox::Infinite, _) | (_, RenderBoundingBox::Infinite) => return RenderBoundingBox::Infinite, + (RenderBoundingBox::Rectangle(existing), RenderBoundingBox::Rectangle(bounds)) => RenderBoundingBox::Rectangle(Quad::combine_bounds(existing, bounds)), + (RenderBoundingBox::None, bounds) | (bounds, RenderBoundingBox::None) => bounds, + }; + } + return combined; + } + other => return other.bounding_box(transform, true), + } + + match text_bounds { + Some(bounds) => RenderBoundingBox::Rectangle(bounds), + None => RenderBoundingBox::None, + } +} + /// Emits one item of text content as SVG, laying out its glyphs and wrapping them in a styled group. fn render_text_item_svg(item: ItemRef<'_, String>, render: &mut SvgRender, render_params: &RenderParams) { let Some(text) = item.element() else { return }; @@ -3958,10 +4001,36 @@ mod tests { .unwrap(); assert!(defs.contains(r#"dx="2" dy="0""#), "{defs}"); - assert!(defs.contains(r#"stdDeviation="1""#), "{defs}"); + assert!(defs.contains(r#"stdDeviation="1 1""#), "{defs}"); assert!(defs.contains(r#"x="-5" y="-5" width="20" height="20""#), "{defs}"); } + #[test] + fn svg_effects_filters_measure_lengths_along_each_axis_of_a_stretched_user_space() { + use graphic_types::EffectOperation; + + let shadow = EffectOperation::DropShadow { + color: Color::BLACK, + offset: DVec2::new(0., 10.), + blur: 4., + spread: 0., + }; + let mut defs = String::new(); + + // A user space stretched 4 times as wide measures the 22 pixel reach as 5.5 units across and 22 down + write_svg_effects_filter( + &mut defs, + &Effects::from_operations([shadow]), + 1., + [DVec2::ZERO, DVec2::splat(10.)], + DAffine2::from_scale(DVec2::new(4., 1.)), + ) + .unwrap(); + + assert!(defs.contains(r#"stdDeviation="1 4""#), "{defs}"); + assert!(defs.contains(r#"x="-5.5" y="-22" width="21" height="54""#), "{defs}"); + } + #[test] fn svg_effects_filters_end_with_the_image_shown() { use graphic_types::EffectOperation; @@ -3976,7 +4045,7 @@ mod tests { // The fill fades a copy of the content, which is blurred in linear light and passed through as the filter's last result assert!(defs.contains(r#""#), "{defs}"); - assert!(defs.contains(r#"in="effect-1" stdDeviation="3" color-interpolation-filters="linearRGB""#), "{defs}"); + assert!(defs.contains(r#"in="effect-1" stdDeviation="3 3" color-interpolation-filters="linearRGB""#), "{defs}"); assert!(defs.ends_with(r#""#), "{defs}"); } } diff --git a/node-graph/libraries/rendering/src/vello_scene_builder.rs b/node-graph/libraries/rendering/src/vello_scene_builder.rs index f96e7b1f796..6120de754b2 100644 --- a/node-graph/libraries/rendering/src/vello_scene_builder.rs +++ b/node-graph/libraries/rendering/src/vello_scene_builder.rs @@ -13,6 +13,9 @@ const SHAPE_TOLERANCE: f64 = 0.1; /// The texture size an effects group's area rounds up to a multiple of, so a group moving across renders keeps reusing the same pooled textures. const EFFECTS_SIZE_CLASS: u32 = 64; +/// The most pixels an effects group's area renders with, as a multiple of the viewport's, beyond which it renders at a lower resolution to bound its memory. +const MAX_EFFECTS_AREA_PER_VIEWPORT_AREA: f64 = 4.; + /// Builds a Vello GPU scene of a fixed size, recording the images it draws so the renderer can supply their textures at render time. pub struct VelloSceneBuilder<'a> { scene: vello_gpu::Scene, @@ -129,6 +132,14 @@ impl<'a> VelloSceneBuilder<'a> { self.view } + /// Draws what `draw` draws placed by `placement` within the view, as content with its own document space for its effects' lengths, like each thumbnail of an atlas. + pub fn draw_placed(&mut self, placement: Affine, draw: impl FnOnce(&mut Self)) { + let outer_view = self.view; + self.view = outer_view * placement; + draw(self); + self.view = outer_view; + } + /// The tolerance Vello GPU expands a stroke drawn with `transform` at, so a stroke outline expanded at it fills identically. pub fn stroke_tolerance(&self, transform: Affine) -> f64 { // Matches `vello_common::flatten::stroke`, including its use of the diagonal coefficients as the scale @@ -249,10 +260,11 @@ impl<'a> VelloSceneBuilder<'a> { return; } - // A region too large for one texture renders at a lower resolution, which the blurs and shadows that make it so large hide. + // A region too large for one texture, or for its memory, renders at a lower resolution, which the blurs and shadows that make it so large hide. // Its size rounds up to a size class so a group that moves between renders keeps reusing the same textures. let max_size = self.max_texture_size.min(u16::MAX as u32); - let resolution = (max_size as f64 / region.width().max(region.height())).min(1.); + let max_area = MAX_EFFECTS_AREA_PER_VIEWPORT_AREA * viewport.area(); + let resolution = (max_size as f64 / region.width().max(region.height())).min((max_area / region.area()).sqrt()).min(1.); let size_along = |length: f64| ((length * resolution).ceil() as u32).next_multiple_of(EFFECTS_SIZE_CLASS).min(max_size); let size = UVec2::new(size_along(region.width()), size_along(region.height())); let origin = region.origin().to_vec2(); diff --git a/node-graph/libraries/wgpu-executor/src/vello_effects.rs b/node-graph/libraries/wgpu-executor/src/vello_effects.rs index 53d6bcd8292..b3dc318df0f 100644 --- a/node-graph/libraries/wgpu-executor/src/vello_effects.rs +++ b/node-graph/libraries/wgpu-executor/src/vello_effects.rs @@ -433,6 +433,9 @@ impl PassRecorder<'_> { /// Grows the shapes in `texture` by `distance` pixels along each axis, in a square like SVG's `feMorphology`. fn dilate(&mut self, texture: wgpu::Texture, distance: f64) -> wgpu::Texture { + // Growing past the texture's whole size changes nothing more + let distance = distance.min(self.size.max_element() as f64); + // Steps that double, then the remainder, so each pass grows the shapes by up to as much as all the passes before it let mut steps = Vec::new(); let (mut covered, mut step) = (0., 1.); diff --git a/node-graph/libraries/wgpu-executor/src/vello_render.rs b/node-graph/libraries/wgpu-executor/src/vello_render.rs index 1004eb7ed39..40acf0c8a30 100644 --- a/node-graph/libraries/wgpu-executor/src/vello_render.rs +++ b/node-graph/libraries/wgpu-executor/src/vello_render.rs @@ -125,7 +125,8 @@ impl VelloRenderer { device, queue, }; - if let Err(error) = context.submit(scene.finish(), max_texture_size, target, TargetAlpha::Straight) { + let (scene, images) = scene.finish(); + if let Err(error) = context.submit(&scene, images, max_texture_size, target, TargetAlpha::Straight) { log::error!("Vello GPU failed to render: {error}"); clear(device, queue, target); } @@ -224,13 +225,16 @@ impl RenderContext<'_> { scene.reset_and_resize(width, height); let mut builder = VelloSceneBuilder::from_vello(scene, view, max_texture_size).with_effects_renderer(self); draw(&mut builder); - let drawn = builder.finish(); + let (scene, images) = builder.finish(); - self.submit(drawn, max_texture_size, target, target_alpha) + let rendered = self.submit(&scene, images, max_texture_size, target, target_alpha); + // Once the render that draws it is submitted, the scene is free to reuse + self.canvas.spare_scenes.push(scene); + rendered } /// Renders a drawn scene, with the images it draws, into `target`, a texture of the scene's size, in a single Vello GPU render. - fn submit(&mut self, (scene, images): (vello_gpu::Scene, Vec), max_texture_size: u32, target: &wgpu::Texture, target_alpha: TargetAlpha) -> Result<(), RenderError> { + fn submit(&mut self, scene: &vello_gpu::Scene, images: Vec, max_texture_size: u32, target: &wgpu::Texture, target_alpha: TargetAlpha) -> Result<(), RenderError> { let device = self.device; let (width, height) = (scene.width(), scene.height()); let size = UVec2::new(width as u32, height as u32); @@ -272,7 +276,7 @@ impl RenderContext<'_> { let mut gpu = self.renderer.gpu.lock().unwrap_or_else(PoisonError::into_inner); let (renderer, resources) = &mut *gpu; let rendered = renderer.render( - &scene, + scene, resources, device, &queue, @@ -292,8 +296,7 @@ impl RenderContext<'_> { queue.submit([encoder.finish()]); drop((gpu, queue)); - // Once the render that draws them is submitted, its scene and effects results are free to reuse - self.canvas.spare_scenes.push(scene); + // Once the render that draws them is submitted, its effects results are free to reuse for image in images { if let SceneImage::Rendered(texture) = image { self.canvas.effect_textures.release(texture); diff --git a/node-graph/nodes/raster/src/adjust.rs b/node-graph/nodes/raster/src/adjust.rs index e6748810672..78f8da84500 100644 --- a/node-graph/nodes/raster/src/adjust.rs +++ b/node-graph/nodes/raster/src/adjust.rs @@ -1,11 +1,25 @@ use core_types::color::Color; +use core_types::list::Item; use graphic_types::Adjustment; use raster_kernels::adjustments::{PreparedAdjustment, adjust_color, adjust_colors}; +use raster_kernels::evaluate_raster_effects; use raster_types::{CPU, Raster}; use vector_types::Gradient; -pub trait Adjust { +pub trait Adjust: Sized { fn adjust(&mut self, adjustment: &Adjustment); + + /// The item with the effects stamped on it carried out, where the element holds the pixels they act on, so an adjustment after them applies to their result. + fn carry_out_effects(item: Item) -> Item { + item + } +} + +/// Applies the adjustment to the item's element, after the effects stamped on it before. +pub fn adjust_item(item: Item, adjustment: &Adjustment) -> Item { + let mut item = T::carry_out_effects(item); + item.element_mut().adjust(adjustment); + item } impl Adjust for Color { @@ -18,6 +32,10 @@ impl Adjust for Raster { fn adjust(&mut self, adjustment: &Adjustment) { adjust_colors(adjustment, &mut self.data_mut().data); } + + fn carry_out_effects(item: Item) -> Item { + evaluate_raster_effects(item) + } } impl Adjust for Gradient { diff --git a/node-graph/nodes/raster/src/adjustments.rs b/node-graph/nodes/raster/src/adjustments.rs index 60762659608..ab83690b515 100644 --- a/node-graph/nodes/raster/src/adjustments.rs +++ b/node-graph/nodes/raster/src/adjustments.rs @@ -1,6 +1,6 @@ #![allow(clippy::too_many_arguments)] -use crate::adjust::Adjust; +use crate::adjust::{Adjust, adjust_item}; use crate::color_lookup_table::{LutParseError, parse_cached}; use core_types::color::Color; use core_types::context::Ctx; @@ -17,11 +17,8 @@ pub use graphic_types::generator::{CellularDistanceFunction, CellularReturnType, use raster_types::{CPU, Image, Raster}; use vector_types::Gradient; -/// Applies the adjustment to the item's element. fn adjust(item: Item, adjustment: Adjustment) -> Item { - let mut item = item; - item.element_mut().adjust(&adjustment); - item + adjust_item(item, &adjustment) } #[node_macro::node(category("Raster: Adjustment"), icon("NodeDesaturate"))] @@ -1464,6 +1461,30 @@ mod tests { } } + #[test] + fn an_adjustment_after_an_effect_applies_to_its_result() { + use core_types::ATTR_TRANSFORM; + use glam::DAffine2; + use graphic_types::{EffectOperation, Effects}; + + // A red shadow cast beside a white square, which desaturating after the shadow turns gray + let shadow = EffectOperation::DropShadow { + color: Color::RED, + offset: DVec2::new(4., 0.), + blur: 0., + spread: 0., + }; + let square = Item::new_from_element(Raster::new_cpu(Image::new(4, 4, Color::WHITE))) + .with_attribute(ATTR_TRANSFORM, DAffine2::from_scale(DVec2::splat(4.))) + .with_attribute(core_types::list::ATTR_EFFECTS, Effects::from_operations([shadow])); + + let desaturated = desaturate((), square, DesaturateMethod::default().into()); + + let shaded = desaturated.element().data.iter().filter(|pixel| pixel.a() > 0.5 && pixel.r() < 0.99).collect::>(); + assert!(!shaded.is_empty(), "the shadow was carried out"); + assert!(shaded.iter().all(|pixel| (pixel.r() - pixel.g()).abs() < 0.01), "the shadow stayed red"); + } + /// Runs the node on one gamma-space gray value (0..255) and returns the gamma-space result on the same scale. fn run_brightness_contrast(value: f32, brightness: f64, contrast: f64, use_classic: bool) -> f32 { let pixel = Color::from_gamma_srgb_channels(value / 255., value / 255., value / 255., 1.); diff --git a/node-graph/nodes/raster/src/blending_nodes.rs b/node-graph/nodes/raster/src/blending_nodes.rs index 6e46550b524..0bffc6772d9 100644 --- a/node-graph/nodes/raster/src/blending_nodes.rs +++ b/node-graph/nodes/raster/src/blending_nodes.rs @@ -1,4 +1,4 @@ -use crate::adjust::Adjust; +use crate::adjust::{Adjust, adjust_item}; use core_types::Ctx; use core_types::blending::BlendMode; use core_types::color::Color; @@ -48,14 +48,12 @@ fn color_overlay( #[default(100.)] opacity: Item, ) -> Item { - let mut image = image; let adjustment = Adjustment::ColorOverlay { color: color.into_element(), blend_mode: blend_mode.into_element(), opacity: opacity.into_element(), }; - image.element_mut().adjust(&adjustment); - image + adjust_item(image, &adjustment) } #[cfg(test)] diff --git a/node-graph/nodes/vectorize/Cargo.toml b/node-graph/nodes/vectorize/Cargo.toml index f906a16bafb..79e45bdd6d1 100644 --- a/node-graph/nodes/vectorize/Cargo.toml +++ b/node-graph/nodes/vectorize/Cargo.toml @@ -19,6 +19,7 @@ workspace = true core-types = { workspace = true } vector-types = { workspace = true } raster-types = { workspace = true } +raster-kernels = { workspace = true } graphic-types = { workspace = true } graphene-hash = { workspace = true } node-macro = { workspace = true } diff --git a/node-graph/nodes/vectorize/src/lib.rs b/node-graph/nodes/vectorize/src/lib.rs index 779bbd4a464..61915c3492b 100644 --- a/node-graph/nodes/vectorize/src/lib.rs +++ b/node-graph/nodes/vectorize/src/lib.rs @@ -9,6 +9,7 @@ use core_types::runtime::Runtime; use core_types::{ATTR_TRANSFORM, CacheHash, Ctx, Poll, RuntimeHandle, SourceId}; use glam::{DAffine2, DVec2}; use graphic_types::Graphic; +use raster_kernels::evaluate_raster_effects; use raster_types::{CPU, Raster}; use std::hash::{DefaultHasher, Hash, Hasher}; use std::time::Duration; @@ -107,10 +108,7 @@ fn vectorize( /// Runs one slice of the trace held in `slot`, starting a new trace if `slot` holds none for these inputs. fn trace_slice(image: &Item>, detail: f64, tolerance: f64, stages: ollev::Stages, max_pixels: Option, slot: &TraceSlot, slice: Duration) -> Poll> { - let image_transform: DAffine2 = image.attribute_cloned_or_default(ATTR_TRANSFORM); - let raster = image.element(); - let (width, height) = (raster.width as usize, raster.height as usize); - if width == 0 || height == 0 { + if image.element().width == 0 || image.element().height == 0 { slot.clear(); return Poll::Final(List::new()); } @@ -120,7 +118,14 @@ fn trace_slice(image: &Item>, detail: f64, tolerance: f64, stages: o (detail.to_bits(), tolerance.to_bits(), max_pixels).hash(&mut hasher); [stages.translucency, stages.strokes, stages.primitives, stages.effects, stages.instancing, stages.path_gradients].hash(&mut hasher); + // The image's effects are carried out once for the whole trace, rather than on each slice let start = || { + let image = evaluate_raster_effects(image.clone()); + let raster = image.element(); + let (width, height) = (raster.width as usize, raster.height as usize); + let image_transform: DAffine2 = image.attribute_cloned_or_default(ATTR_TRANSFORM); + let pixels_to_document = image_transform * DAffine2::from_scale(DVec2::new(1. / width as f64, 1. / height as f64)); + let source = ollev::Image { pixels: ollev::Pixels::RgbaF32 { data: bytemuck::cast_slice(&raster.data), @@ -138,11 +143,10 @@ fn trace_slice(image: &Item>, detail: f64, tolerance: f64, stages: o stages, ..Default::default() }; - ollev::Tracing::new(&source, &options) + ollev::Tracing::new(&source, &options).map(|tracing| (tracing, pixels_to_document)) }; - let pixels_to_document = image_transform * DAffine2::from_scale(DVec2::new(1. / width as f64, 1. / height as f64)); - slot.poll(hasher.finish(), slice, start, |model| convert::model_to_graphic(model, pixels_to_document)) + slot.poll(hasher.finish(), slice, start, convert::model_to_graphic) } #[cfg(test)] diff --git a/node-graph/nodes/vectorize/src/slices.rs b/node-graph/nodes/vectorize/src/slices.rs index e1cbc681694..158037be5d2 100644 --- a/node-graph/nodes/vectorize/src/slices.rs +++ b/node-graph/nodes/vectorize/src/slices.rs @@ -1,6 +1,7 @@ use core_types::Poll; use core_types::list::List; use core_types::runtime::Instant; +use glam::DAffine2; use graphic_types::Graphic; use ollev::model::Model; use std::ops::ControlFlow; @@ -24,24 +25,33 @@ struct Trace { tracing: Option, /// Why the trace failed, kept so the same inputs are not traced again. failed: Option, + /// Places the traced image's pixels in the document. + pixels_to_document: DAffine2, shown: List, /// The revision of the scene in `shown`. revision: Option, } impl TraceSlot { - /// Advances the trace of `inputs` by `slice`, replacing any trace of other inputs with one from `start`. + /// Advances the trace of `inputs` by `slice`, replacing any trace of other inputs with one from `start`, which also gives where the traced pixels lie. /// Returns the latest showable scene, partial until the trace is done. - pub fn poll(&self, inputs: u64, slice: Duration, start: impl FnOnce() -> Result, convert: impl Fn(&Model) -> List) -> Poll> { + pub fn poll( + &self, + inputs: u64, + slice: Duration, + start: impl FnOnce() -> Result<(ollev::Tracing, DAffine2), ollev::TraceError>, + convert: impl Fn(&Model, DAffine2) -> List, + ) -> Poll> { // Taken out of the slot so a panicking slice leaves it empty let mut held = self.lock(); let mut trace = match held.take().filter(|trace| trace.inputs == inputs) { Some(trace) => trace, None => match start() { - Ok(tracing) => Trace { + Ok((tracing, pixels_to_document)) => Trace { inputs, tracing: Some(tracing), failed: None, + pixels_to_document, shown: List::new(), revision: None, }, @@ -54,7 +64,7 @@ impl TraceSlot { let step = tracing.step(|_| if Instant::now() >= deadline { ControlFlow::Break(()) } else { ControlFlow::Continue(()) }); match step { Ok(ollev::Step::Done(model)) => { - trace.shown = convert(&model); + trace.shown = convert(&model, trace.pixels_to_document); trace.tracing = None; } Err(error) => { @@ -66,7 +76,7 @@ impl TraceSlot { && Some(progress.revision()) != trace.revision && let Some(model) = progress.model() { - trace.shown = convert(&model); + trace.shown = convert(&model, trace.pixels_to_document); trace.revision = Some(progress.revision()); } }