mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-06 08:13:40 -10:00
Implement Graphene base graph UI
This commit is contained in:
1 parent
13bb1687b9
commit
df1434ddff
50 files changed
+1858
-4480
No files matched your search
@@ -8,6 +8,7 @@ use graph_craft::document::value::{RenderOutput, RenderOutputType, TaggedValue};
|
||||
use graph_craft::document::{NodeId, NodeNetwork};
|
||||
use graph_craft::graphene_compiler::{CompileError, CompiledNetwork, Compiler};
|
||||
use graph_craft::proto::GraphErrors;
|
||||
use graph_ui::scene::{GraphUiScene, GraphUiSceneDelta};
|
||||
use graphene_hash::CacheHash;
|
||||
use graphene_std::application_io::{ApplicationIo, ExportFormat, Texture};
|
||||
use graphene_std::ops::ConvertAsync;
|
||||
@@ -18,7 +19,7 @@ use graphene_std::runtime::AsyncRuntime;
|
||||
use graphene_std::vector::style::RenderMode;
|
||||
use graphene_std::{Context, NodeError, Poll};
|
||||
use interpreted_executor::dynamic_executor::{DynamicExecutor, OutputHandle};
|
||||
use interpreted_executor::util::wrap_network_with_render_node_in_scope;
|
||||
use interpreted_executor::util::{wrap_network_in_scope, wrap_network_with_render_node_in_scope};
|
||||
use observer::Observer;
|
||||
pub use observer::{InspectResult, InspectedContext, Observation, ObservedConnector, ObservedValue};
|
||||
use std::sync::Arc;
|
||||
@@ -71,7 +72,6 @@ pub struct NodeRuntime {
|
||||
|
||||
thumbnails: Thumbnails,
|
||||
observer: Observer<ObservationKey>,
|
||||
/// Whether an observation changed since its results were last sent.
|
||||
observations_changed: bool,
|
||||
|
||||
preprocessor: preprocessor::Preprocessor,
|
||||
@@ -87,6 +87,12 @@ pub struct NodeRuntime {
|
||||
/// The latest rendered overlays.
|
||||
overlays_texture: Option<Texture>,
|
||||
|
||||
graph_ui_scene: GraphUiScene,
|
||||
graph_ui_output: OutputHandle,
|
||||
graph_ui_network_hash: Option<u64>,
|
||||
graph_ui_texture: Option<Texture>,
|
||||
graph_ui_redrawn: bool,
|
||||
|
||||
/// Cached surface for Wasm viewport rendering (reused across frames)
|
||||
#[cfg(all(target_family = "wasm", feature = "wasm"))]
|
||||
wasm_canvas_cache: CanvasSurfaceHandle,
|
||||
@@ -113,6 +119,7 @@ pub enum GraphRuntimeRequest {
|
||||
},
|
||||
ThumbnailsInView {
|
||||
layers: HashSet<NodeId>,
|
||||
layers_panel: HashSet<NodeId>,
|
||||
document_id: DocumentId,
|
||||
},
|
||||
RenderOverlays(OverlayContext),
|
||||
@@ -120,6 +127,7 @@ pub enum GraphRuntimeRequest {
|
||||
key: ObservationKey,
|
||||
observation: Option<Observation>,
|
||||
},
|
||||
GraphUi(GraphUiSceneDelta),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -192,6 +200,12 @@ impl NodeRuntime {
|
||||
overlays_canvas: VelloCanvas::default(),
|
||||
overlays_texture: None,
|
||||
|
||||
graph_ui_scene: GraphUiScene::default(),
|
||||
graph_ui_output: OutputHandle::default(),
|
||||
graph_ui_network_hash: None,
|
||||
graph_ui_texture: None,
|
||||
graph_ui_redrawn: false,
|
||||
|
||||
#[cfg(all(target_family = "wasm", feature = "wasm"))]
|
||||
wasm_canvas_cache: CanvasSurfaceHandle::new(),
|
||||
}
|
||||
@@ -254,8 +268,8 @@ impl NodeRuntime {
|
||||
None => self.send_export(id, ExecutionResult::Uncompiled),
|
||||
}
|
||||
}
|
||||
GraphRuntimeRequest::ThumbnailsInView { layers, document_id } => {
|
||||
self.thumbnails.set_in_view(layers);
|
||||
GraphRuntimeRequest::ThumbnailsInView { layers, layers_panel, document_id } => {
|
||||
self.thumbnails.set_in_view(layers, layers_panel);
|
||||
self.pending_thumbnails = Some(document_id);
|
||||
}
|
||||
GraphRuntimeRequest::Observe { key, observation } => {
|
||||
@@ -266,13 +280,13 @@ impl NodeRuntime {
|
||||
self.overlays = Some(context);
|
||||
self.overlays_stale = true;
|
||||
}
|
||||
GraphRuntimeRequest::GraphUi(delta) => self.graph_ui_scene.apply(delta),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> Option<Texture> {
|
||||
let dirty = self.dirty.swap(false, Ordering::Acquire);
|
||||
let mut texture = None;
|
||||
|
||||
if dirty {
|
||||
for (id, render_config, output) in std::mem::take(&mut self.exports) {
|
||||
@@ -287,7 +301,10 @@ impl NodeRuntime {
|
||||
&& self.output.is_some()
|
||||
{
|
||||
let mut responses = VecDeque::new();
|
||||
self.thumbnails.render_stale(&self.executor, &self.editor_api, &mut responses).await;
|
||||
let rendered = self.thumbnails.render_stale(&self.executor, &self.editor_api, &mut responses).await;
|
||||
if rendered && let Some(request) = self.pending.filter(|request| request.graph_ui_config.is_some()) {
|
||||
self.draw_graph_ui(request);
|
||||
}
|
||||
if !responses.is_empty() {
|
||||
self.sender.send(NodeGraphUpdate::Thumbnails { responses, document_id }).expect("Failed to send response");
|
||||
}
|
||||
@@ -306,10 +323,7 @@ impl NodeRuntime {
|
||||
self.run_viewport(viewport).await;
|
||||
}
|
||||
|
||||
// Overlays that changed without new artwork are drawn over the artwork already shown
|
||||
if self.overlays_stale {
|
||||
texture = self.composite_viewport_frame();
|
||||
}
|
||||
let texture = self.composite_viewport_frame();
|
||||
|
||||
(self.editor_wake)();
|
||||
texture
|
||||
@@ -340,11 +354,65 @@ impl NodeRuntime {
|
||||
}
|
||||
|
||||
self.respond(request, result, observations).await;
|
||||
self.draw_graph_ui(request);
|
||||
}
|
||||
|
||||
fn draw_graph_ui(&mut self, request: ExecutionRequest) {
|
||||
self.graph_ui_redrawn = true;
|
||||
let Some(mut config) = request.graph_ui_config else {
|
||||
self.graph_ui_texture = None;
|
||||
self.graph_ui_network_hash = None;
|
||||
self.graph_ui_output = OutputHandle::default();
|
||||
return;
|
||||
};
|
||||
if !matches!(self.viewport_artwork, Some(ViewportArtwork::Texture(_))) {
|
||||
config.fade_artwork = 1.;
|
||||
}
|
||||
|
||||
let scene = self.graph_ui_scene.clone();
|
||||
let thumbnails = self.thumbnails.graphics();
|
||||
let graph_ui_network = graph_ui::network(&scene, &config, &thumbnails);
|
||||
let hash = graph_ui_network.current_hash();
|
||||
if self.graph_ui_network_hash != Some(hash) {
|
||||
let mut graph_ui_network = wrap_network_in_scope(graph_ui_network, self.editor_api.clone(), self.async_runtime().handle());
|
||||
self.preprocessor.preprocess(&mut graph_ui_network, &|_| None).expect("The network of the node graph preprocesses");
|
||||
let graph_ui_network = Compiler {}.compile_single(graph_ui_network).expect("The network of the node graph compiles").network;
|
||||
let replaced = std::mem::take(&mut self.graph_ui_output);
|
||||
self.graph_ui_output = self.executor.replace(replaced, graph_ui_network).expect("The network of the node graph is typed");
|
||||
self.graph_ui_network_hash = Some(hash);
|
||||
self.async_runtime().update_sources(self.executor.sources());
|
||||
}
|
||||
|
||||
let context = graph_ui::render_config(config.view, request.render_config).into_context();
|
||||
self.graph_ui_texture = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| self.executor.execute(&self.graph_ui_output, context))) {
|
||||
Ok(Ok(Poll::Final(TaggedValue::RenderOutput(RenderOutput {
|
||||
data: RenderOutputType::Texture(frame),
|
||||
..
|
||||
})))) => Some(frame),
|
||||
Ok(Ok(Poll::Error(error))) => {
|
||||
log::error!("Failed to draw the node graph: {}", error.error());
|
||||
None
|
||||
}
|
||||
Ok(Ok(_)) => None,
|
||||
Ok(Err(error)) => {
|
||||
log::error!("Failed to draw the node graph: {error}");
|
||||
None
|
||||
}
|
||||
Err(payload) => {
|
||||
let message = payload.downcast_ref::<&str>().copied().or_else(|| payload.downcast_ref::<String>().map(String::as_str));
|
||||
log::error!("Panicked while drawing the node graph: {}", message.unwrap_or("no message"));
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Draws the latest overlays over the viewport's artwork, or alone above SVG artwork, presenting the frame on web.
|
||||
fn composite_viewport_frame(&mut self) -> Option<Texture> {
|
||||
let artwork = self.viewport_artwork.as_ref()?;
|
||||
if !std::mem::take(&mut self.graph_ui_redrawn) && !self.overlays_stale {
|
||||
return None;
|
||||
}
|
||||
if self.viewport_artwork.is_none() && self.graph_ui_texture.is_none() {
|
||||
return None;
|
||||
}
|
||||
let executor = self.editor_api.application_io.as_ref().and_then(|application_io| application_io.gpu_executor())?;
|
||||
|
||||
if std::mem::take(&mut self.overlays_stale)
|
||||
@@ -356,24 +424,34 @@ impl NodeRuntime {
|
||||
.ok();
|
||||
}
|
||||
|
||||
let frame = match (artwork, &self.overlays_texture) {
|
||||
(ViewportArtwork::Texture(artwork), Some(overlays)) => {
|
||||
let (width, height) = (artwork.width(), artwork.height());
|
||||
let artwork = match &self.viewport_artwork {
|
||||
Some(ViewportArtwork::Texture(artwork)) => Some(artwork),
|
||||
Some(ViewportArtwork::Svg) | None => None,
|
||||
};
|
||||
let overlays = self.overlays_texture.as_ref().filter(|_| self.graph_ui_texture.is_none());
|
||||
let frame = match (artwork, &self.graph_ui_texture, overlays) {
|
||||
(None, None, None) => return None,
|
||||
(Some(frame), None, None) | (None, Some(frame), None) | (None, None, Some(frame)) => frame.clone(),
|
||||
(artwork, graph_ui, overlays) => {
|
||||
// The node graph is drawn at the viewport's size, which artwork from before a resize doesn't have
|
||||
let base = graph_ui.as_ref().or(artwork).or(overlays)?;
|
||||
let (width, height) = (base.width(), base.height());
|
||||
let max_texture_size = executor.context().device.limits().max_texture_dimension_2d;
|
||||
let overlays_to_artwork = kurbo::Affine::scale_non_uniform(width as f64 / overlays.width() as f64, height as f64 / overlays.height() as f64);
|
||||
|
||||
let mut scene = VelloSceneBuilder::new(width as u16, height as u16, kurbo::Affine::IDENTITY, max_texture_size);
|
||||
scene.draw_texture(artwork.clone(), ImageSampler::new(), kurbo::Affine::IDENTITY);
|
||||
scene.draw_texture(overlays.clone(), ImageSampler::new(), overlays_to_artwork);
|
||||
for layer in [artwork, graph_ui.as_ref()].into_iter().flatten() {
|
||||
scene.draw_texture(layer.clone(), ImageSampler::new(), kurbo::Affine::IDENTITY);
|
||||
}
|
||||
if let Some(overlays) = overlays {
|
||||
let overlays_to_frame = kurbo::Affine::scale_non_uniform(width as f64 / overlays.width() as f64, height as f64 / overlays.height() as f64);
|
||||
scene.draw_texture(overlays.clone(), ImageSampler::new(), overlays_to_frame);
|
||||
}
|
||||
|
||||
executor
|
||||
.render_vello_scene_on_canvas(&mut self.overlays_canvas, scene)
|
||||
.inspect_err(|error| log::error!("Failed to draw the overlays over the artwork: {error:?}"))
|
||||
.unwrap_or_else(|_| artwork.clone())
|
||||
.inspect_err(|error| log::error!("Failed to draw the viewport's frame: {error:?}"))
|
||||
.unwrap_or_else(|_| base.clone())
|
||||
}
|
||||
(ViewportArtwork::Texture(artwork), None) => artwork.clone(),
|
||||
(ViewportArtwork::Svg, Some(overlays)) => overlays.clone(),
|
||||
(ViewportArtwork::Svg, None) => return None,
|
||||
};
|
||||
|
||||
#[cfg(all(target_family = "wasm", feature = "wasm"))]
|
||||
@@ -418,10 +496,14 @@ impl NodeRuntime {
|
||||
}
|
||||
|
||||
fn evaluate(&mut self, output: &OutputHandle, request: ExecutionRequest) -> Option<ExecutionResult> {
|
||||
let ExecutionRequest { execution_id, mut render_config } = request;
|
||||
let ExecutionRequest {
|
||||
execution_id,
|
||||
mut render_config,
|
||||
graph_ui_config,
|
||||
} = request;
|
||||
|
||||
// Render via the SVG pipeline even though raster was requested if SVG Preview render mode is active
|
||||
if render_config.export_format == ExportFormat::Raster && render_config.render_mode == RenderMode::SvgPreview {
|
||||
// Render via the SVG pipeline even though raster was requested if SVG Preview render mode is active and the graph, isn't open
|
||||
if render_config.export_format == ExportFormat::Raster && render_config.render_mode == RenderMode::SvgPreview && graph_ui_config.is_none() {
|
||||
render_config.export_format = ExportFormat::Svg;
|
||||
}
|
||||
|
||||
@@ -483,7 +565,7 @@ impl NodeRuntime {
|
||||
RenderOutputType::Svg { .. } => request.render_config.export_format == ExportFormat::Raster,
|
||||
RenderOutputType::Buffer { .. } => false,
|
||||
},
|
||||
None => false,
|
||||
None => request.graph_ui_config.is_some(),
|
||||
})
|
||||
.then(|| ViewportCanvasInfo {
|
||||
id: self.wasm_canvas_cache.id(),
|
||||
@@ -502,14 +584,6 @@ impl NodeRuntime {
|
||||
self.sender.send(NodeGraphUpdate::ExecutionResponse(Box::new(response))).expect("Failed to send response");
|
||||
}
|
||||
|
||||
/// Reads one observation from the last evaluation, apart from the observations the runtime keeps.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn observe(&self, observation: Observation) -> Option<ObservedValue> {
|
||||
let mut observer = Observer::default();
|
||||
observer.set((), Some(observation));
|
||||
observer.evaluate(&self.executor, self.old_graph.as_ref()).remove(&())?.data
|
||||
}
|
||||
|
||||
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");
|
||||
@@ -581,6 +655,14 @@ impl NodeRuntime {
|
||||
|
||||
dropped_thumbnails
|
||||
}
|
||||
|
||||
/// Reads one observation from the last evaluation, apart from the observations the runtime keeps.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn observe(&self, observation: Observation) -> Option<ObservedValue> {
|
||||
let mut observer = Observer::default();
|
||||
observer.set((), Some(observation));
|
||||
observer.evaluate(&self.executor, self.old_graph.as_ref()).remove(&())?.data
|
||||
}
|
||||
}
|
||||
|
||||
impl ExecutionResult {
|
||||
@@ -677,12 +759,16 @@ mod tests {
|
||||
|
||||
/// Sends a network update and a request to evaluate it at a viewport of `resolution` pixels.
|
||||
fn send_update_at(editor: &mut EditorTestUtils, resolution: UVec2) {
|
||||
send_update_with_graph_ui(editor, resolution, None);
|
||||
}
|
||||
|
||||
fn send_update_with_graph_ui(editor: &mut EditorTestUtils, resolution: UVec2, graph_ui_config: Option<GraphUiConfig>) {
|
||||
let portfolio = editor.portfolio_message_handler_mut();
|
||||
let document_id = portfolio.active_document_id.unwrap();
|
||||
let (executor, documents) = (&mut portfolio.executor, &mut portfolio.documents);
|
||||
let document = documents.get_mut(&document_id).unwrap();
|
||||
executor
|
||||
.submit_node_graph_evaluation(document, document_id, resolution, 1., Default::default(), Vec::new(), true, DVec2::ZERO)
|
||||
.submit_node_graph_evaluation(document, document_id, resolution, 1., Default::default(), Vec::new(), true, DVec2::ZERO, graph_ui_config)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@@ -763,7 +849,7 @@ mod tests {
|
||||
let document = portfolio.documents.get_mut(&document_id).unwrap();
|
||||
portfolio
|
||||
.executor
|
||||
.submit_current_node_graph_evaluation(document, document_id, UVec2::ONE, 1., Default::default(), DVec2::ZERO)
|
||||
.submit_current_node_graph_evaluation(document, document_id, UVec2::ONE, 1., Default::default(), DVec2::ZERO, None)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@@ -774,7 +860,7 @@ mod tests {
|
||||
|
||||
/// The ID of the viewport request the runtime evaluates, which only changes once it has read a newer one.
|
||||
fn last_request(editor: &EditorTestUtils) -> Option<u64> {
|
||||
editor.runtime.pending.map(|request| request.execution_id)
|
||||
editor.runtime.pending.as_ref().map(|request| request.execution_id)
|
||||
}
|
||||
|
||||
fn respond_to_last_request(editor: &mut EditorTestUtils, result: ExecutionResult) -> Vec<FrontendMessage> {
|
||||
@@ -831,7 +917,7 @@ mod tests {
|
||||
let (executor, documents) = (&mut portfolio.executor, &mut portfolio.documents);
|
||||
let document = documents.get_mut(&document_id).unwrap();
|
||||
let message = executor
|
||||
.submit_current_node_graph_evaluation(document, document_id, UVec2::ONE, scale, Default::default(), DVec2::ZERO)
|
||||
.submit_current_node_graph_evaluation(document, document_id, UVec2::ONE, scale, Default::default(), DVec2::ZERO, None)
|
||||
.unwrap();
|
||||
editor.dispatch(message);
|
||||
}
|
||||
@@ -911,7 +997,7 @@ mod tests {
|
||||
|
||||
let mut editor = editor().await;
|
||||
let layer = editor.active_document().metadata().all_layers().next().unwrap().to_node();
|
||||
editor.runtime.thumbnails.set_in_view(HashSet::from([layer]));
|
||||
editor.runtime.thumbnails.set_in_view(HashSet::from([layer]), HashSet::from([layer]));
|
||||
send_update(&mut editor);
|
||||
assert!(!thumbnail_stale(&mut editor, layer).await);
|
||||
|
||||
|
||||
Reference in new issue
Block a user