Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions document/migrations/nodes/src/graphene_std/renames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ rename!("graphene_std::render_background::RenderBackgroundNode"@1 => @2);
rename!("graphene_std::render_cache::RenderOutputCacheNode"@1 => @2);
rename!("graphene_std::render_node::RenderNode"@1 => @2);
rename!("graphene_std::render_pixel_preview::RenderPixelPreviewNode"@1 => @2);

// Version 3 adds the scope and source ID inputs the render node spawns its readbacks with, which the compiler inserts along with the node
rename!("graphene_std::render_node::RenderNode"@2 => @3);
66 changes: 38 additions & 28 deletions editor/src/node_graph_executor/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ impl NodeRuntime {
None
};

self.resolve_deferred_images(&mut result).await;
self.respond(request.execution_id, result, observations).await;
texture
}
Expand Down Expand Up @@ -451,7 +450,6 @@ impl NodeRuntime {
let (buffer, width, height) = self.read_back(texture.clone()).await.to_flat_u8();
*data = RenderOutputType::Buffer { data: buffer, width, height };
}
self.resolve_deferred_images(&mut result).await;
let observations = self.observer.evaluate(&self.executor, self.old_graph.as_ref());
self.respond(request.execution_id, result, observations).await;
}
Expand Down Expand Up @@ -537,32 +535,6 @@ impl NodeRuntime {
observer.evaluate(&self.executor, self.old_graph.as_ref()).remove(&())?.data
}

/// Puts what the GPU rendered for an SVG result into it, in place of the placeholders standing in for each:
/// the maps of effects' coverages its filters take, and the images of content with effects SVG filters can't carry out.
async fn resolve_deferred_images(&self, result: &mut ExecutionResult) {
let Some(RenderOutput {
data: RenderOutputType::Svg {
svg,
image_data,
effect_maps,
deferred_images,
},
..
}) = result.render_output()
else {
return;
};
if effect_maps.is_empty() && deferred_images.is_empty() {
return;
}
let Some(executor) = self.editor_api.application_io.as_ref().and_then(|application_io| application_io.gpu_executor()) else {
return;
};

executor.resolve_effect_maps(svg, std::mem::take(effect_maps)).await;
executor.resolve_deferred_svg_images(svg, image_data, std::mem::take(deferred_images)).await;
}

async fn read_back(&self, texture: Texture) -> Raster<CPU> {
let application_io = self.editor_api.application_io.as_ref().unwrap();
let executor = application_io.gpu_executor().expect("GPU executor should be available when we receive a texture");
Expand Down Expand Up @@ -1091,6 +1063,44 @@ mod tests {
}
}

#[tokio::test]
async fn an_svg_export_draws_effects_svg_filters_cannot_carry_out_as_images_the_gpu_renders() {
use crate::messages::portfolio::document::node_graph::document_node_definitions::DefinitionIdentifier;
use graphene_std::blending_nodes::effects::drop_shadow;
use graphene_std::raster_nodes::adjustments::black_and_white;

let mut editor = EditorTestUtils::create();
editor.new_document().await;
editor.draw_rect(0., 0., 100., 100.).await;

// Following a shadow, Black & White can't fold into the paints, and no SVG filter mixes the channels as it does, so the rectangle is drawn as an image the GPU renders of it
let layer = editor.active_document().metadata().all_layers().next().unwrap();
for node in [drop_shadow::IDENTIFIER, black_and_white::IDENTIFIER] {
let node_type = DefinitionIdentifier::ProtoNode(node);
editor.handle_message(NodeGraphMessage::CreateNodeInLayerWithTransaction { node_type, layer }).await;
}

// The export waits for the render node to read the image back into the SVG
send_export(&mut editor, FileType::Svg);
let mut saved = None;
for _ in 0..100 {
editor.runtime.update();
editor.runtime.run().await;
saved = editor.dispatch(Message::Wake).into_iter().find_map(|message| match message {
FrontendMessage::TriggerSaveFile { content, .. } => Some(content),
_ => None,
});
if saved.is_some() {
break;
}
}
let Some(saved) = saved else { panic!("the export never finished") };

let svg = String::from_utf8(saved.into_vec()).expect("the export should be SVG text");
assert!(svg.contains("data:image/png;base64,"), "the image should be embedded: {svg}");
assert!(!svg.contains("deferred-image"), "the markup standing in for the image should be replaced: {svg}");
}

#[tokio::test]
async fn exporting_a_graph_that_fails_to_compile_says_it_failed() {
let mut editor = editor().await;
Expand Down
22 changes: 1 addition & 21 deletions node-graph/graph-craft/src/document/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use graphic_types::vector_types::vector::style::DashPattern;
use graphic_types::vector_types::vector::style::{Gradient, GradientRamp};
use graphic_types::vector_types::vector::{self, ReferencePoint};
use graphic_types::{Artboard, Graphic, Vector};
use rendering::deferred_image::{DeferredImagePlacement, EffectMapPlacement};
use rendering::{CanvasImage, RenderMetadata};
use std::fmt::Display;
use std::hash::Hash;
Expand Down Expand Up @@ -921,12 +920,6 @@ pub enum RenderOutputType {
svg: String,
/// The images the SVG draws to canvases, by the canvas ID their placeholders name.
image_data: Vec<(u64, CanvasImage)>,
/// Maps of effects' coverages the GPU rendered for the SVG's filters, which go in place of their placeholders once read back.
#[serde(skip)]
effect_maps: Vec<(EffectMapPlacement, graphene_application_io::Texture)>,
/// Images the GPU rendered of content with effects SVG filters can't carry out, which go in place of the markup standing in for that content once read back.
#[serde(skip)]
deferred_images: Vec<(DeferredImagePlacement, graphene_application_io::Texture)>,
},
#[cfg(target_family = "wasm")]
CanvasFrame { canvas_id: u64, resolution: DVec2 },
Expand All @@ -942,22 +935,9 @@ impl CacheHash for RenderOutputType {
width.cache_hash(state);
height.cache_hash(state);
}
Self::Svg {
svg,
image_data,
effect_maps,
deferred_images,
} => {
Self::Svg { svg, image_data } => {
svg.cache_hash(state);
image_data.cache_hash(state);
for (placement, texture) in effect_maps {
placement.id.cache_hash(state);
texture.hash(state);
}
for (placement, texture) in deferred_images {
placement.id.cache_hash(state);
texture.hash(state);
}
}
#[cfg(target_family = "wasm")]
Self::CanvasFrame { canvas_id, resolution } => {
Expand Down
11 changes: 1 addition & 10 deletions node-graph/graphene-cli/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,7 @@ pub(crate) async fn render_frame(
// Execute the graph and extract its output
match execute_until_settled(executor, runtime, spawner, render_config).await? {
TaggedValue::RenderOutput(output) => match output.data {
RenderOutputType::Svg {
mut svg,
mut image_data,
effect_maps,
deferred_images,
} => {
wgpu_executor.resolve_effect_maps(&mut svg, effect_maps).await;
wgpu_executor.resolve_deferred_svg_images(&mut svg, &mut image_data, deferred_images).await;
Ok(RenderedFrame::Svg(svg))
}
RenderOutputType::Svg { svg, .. } => Ok(RenderedFrame::Svg(svg)),
RenderOutputType::Texture(texture) => {
// Convert GPU texture to CPU buffer
let gpu_raster = Raster::<GPU>::new_gpu(texture);
Expand Down
9 changes: 7 additions & 2 deletions node-graph/interpreted-executor/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use graph_craft::application_io::PlatformEditorApi;
use graph_craft::document::value::TaggedValue;
use graph_craft::document::{DocumentNode, DocumentNodeImplementation, NodeInput, NodeNetwork};
use graph_craft::document::{DocumentNode, DocumentNodeImplementation, DocumentNodeMetadata, NodeInput, NodeNetwork};
use graph_craft::generic;
use graph_craft::{concrete, item};
use graphene_std::Context;
Expand Down Expand Up @@ -42,7 +42,12 @@ pub fn wrap_network_in_scope(mut network: NodeNetwork, editor_api: Arc<PlatformE
},
DocumentNode {
call_argument: concrete!(Context),
inputs: vec![NodeInput::node(NodeId(0), 0), NodeInput::scope(graphene_std::platform_application_io::wgpu_executor::IDENTIFIER)],
inputs: vec![
NodeInput::node(NodeId(0), 0),
NodeInput::scope(graphene_std::platform_application_io::wgpu_executor::IDENTIFIER),
NodeInput::scope("runtime"),
NodeInput::Reflection(DocumentNodeMetadata::SourceId),
],
implementation: DocumentNodeImplementation::ProtoNode(graphene_std::render_node::render::IDENTIFIER),
compile_metadata: Some(graphene_std::NodeCompileMetadata {
extract: ContextFeatures::FOOTPRINT | ContextFeatures::VARARGS,
Expand Down
17 changes: 11 additions & 6 deletions node-graph/libraries/wgpu-executor/src/deferred_svg_images.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::WgpuExecutor;
use core_types::ops::ConvertAsync;
use core_types::transform::Footprint;
use raster_types::{CPU, Raster, Texture};
use raster_types::{Raster, Texture};
use rendering::CanvasImage;
use rendering::deferred_image::{DeferredImage, DeferredImagePlacement, EffectMap, EffectMapPlacement, MAX_DEFERRED_IMAGE_PIXELS, substitute_deferred_image, substitute_effect_map};

Expand All @@ -27,10 +26,13 @@ impl WgpuExecutor {
}

/// Reads back the maps of effects' coverages, putting each in `svg` in place of its placeholder.
/// Maps that fail to read back keep their placeholders.
pub async fn resolve_effect_maps(&self, svg: &mut String, maps: Vec<(EffectMapPlacement, Texture)>) {
for (placement, texture) in maps {
let map: Raster<CPU> = Raster::new_gpu(texture).convert(Footprint::BOUNDLESS, self.clone()).await;
substitute_effect_map(svg, &placement, &map);
match self.read_back(Raster::new_gpu(texture)).await {
Ok(map) => substitute_effect_map(svg, &placement, &map),
Err(error) => log::error!("Failed to read back a map of an effect's coverage for an SVG filter: {error}"),
}
}
}

Expand All @@ -55,10 +57,13 @@ impl WgpuExecutor {
}

/// Reads back the images of deferred content, putting each in `svg` in place of the markup standing in for its content.
/// Content with an image that fails to read back keeps its markup.
pub async fn resolve_deferred_svg_images(&self, svg: &mut String, image_data: &mut Vec<(u64, CanvasImage)>, images: Vec<(DeferredImagePlacement, Texture)>) {
for (placement, texture) in images {
let image: Raster<CPU> = Raster::new_gpu(texture).convert(Footprint::BOUNDLESS, self.clone()).await;
substitute_deferred_image(svg, image_data, &placement, &image);
match self.read_back(Raster::new_gpu(texture)).await {
Ok(image) => substitute_deferred_image(svg, image_data, &placement, &image),
Err(error) => log::error!("Failed to read back an image of content with effects SVG can't express: {error}"),
}
}
}
}
21 changes: 14 additions & 7 deletions node-graph/libraries/wgpu-executor/src/texture_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,27 @@ impl ConvertAsync<List<Raster<CPU>>, WgpuExecutor> for List<Raster<GPU>> {
}
}

/// Converts single GPU raster to CPU by downloading texture data
impl ConvertAsync<Raster<CPU>, WgpuExecutor> for Raster<GPU> {
async fn convert(self, _: Footprint, executor: WgpuExecutor) -> Raster<CPU> {
let device = &executor.context().device;
let queue = &executor.context().queue;
impl WgpuExecutor {
/// Downloads the raster's texture data, or gives the error mapping its buffer for reading failed with.
pub(crate) async fn read_back(&self, raster: Raster<GPU>) -> Result<Raster<CPU>, wgpu::BufferAsyncError> {
let device = &self.context().device;
let queue = &self.context().queue;

let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("single_texture_download_encoder"),
});

let converter = RasterGpuToRasterCpuConverter::new(&executor, &mut encoder, self);
let converter = RasterGpuToRasterCpuConverter::new(self, &mut encoder, raster);

queue.submit([encoder.finish()]);

converter.convert(device).await.expect("Failed to download texture data")
converter.convert(device).await
}
}

/// Converts single GPU raster to CPU by downloading texture data
impl ConvertAsync<Raster<CPU>, WgpuExecutor> for Raster<GPU> {
async fn convert(self, _: Footprint, executor: WgpuExecutor) -> Raster<CPU> {
executor.read_back(self).await.expect("Failed to download texture data")
}
}
1 change: 1 addition & 0 deletions node-graph/nodes/gstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ vectorize-nodes = { workspace = true }

# Workspace dependencies
log = { workspace = true }
futures = { workspace = true }
glam = { workspace = true }
node-macro = { workspace = true }
reqwest = { workspace = true }
Expand Down
9 changes: 1 addition & 8 deletions node-graph/nodes/gstd/src/render_background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ fn render_background(
RenderOutputType::Svg {
svg: foreground_svg,
image_data: foreground_images,
effect_maps,
deferred_images,
} => {
let mut render = SvgRender::new();

Expand Down Expand Up @@ -116,12 +114,7 @@ fn render_background(
let svg = format!("{}{}", background.svg, foreground_svg);
let image_data = foreground_images;

RenderOutputType::Svg {
svg,
image_data,
effect_maps,
deferred_images,
}
RenderOutputType::Svg { svg, image_data }
}
_ => unreachable!("Render background node received unsupported render output type"),
};
Expand Down
Loading
Loading