mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-19 19:08:05 +08:00
Cut over to the graphene execution model
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
use futures::executor::block_on;
|
||||
use graph_craft::document::value::{RenderOutputType, TaggedValue, UVec2};
|
||||
use graph_craft::graphene_compiler::Executor;
|
||||
use graphene_std::application_io::{ExportFormat, RenderConfig, TimingInformation};
|
||||
use graphene_std::core_types::ops::Convert;
|
||||
use graphene_std::core_types::gpoll::GPoll;
|
||||
use graphene_std::core_types::ops::ConvertAsync;
|
||||
use graphene_std::core_types::transform::Footprint;
|
||||
use graphene_std::raster_types::{CPU, GPU, Raster};
|
||||
use interpreted_executor::dynamic_executor::DynamicExecutor;
|
||||
@@ -10,6 +12,19 @@ use std::io::Cursor;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
fn execute_to_final(executor: &DynamicExecutor, render_config: RenderConfig) -> Result<TaggedValue, Box<dyn Error>> {
|
||||
match executor.execute(render_config)? {
|
||||
GPoll::Final(value) => Ok(value),
|
||||
GPoll::Fallback(boxed) => {
|
||||
let (value, error) = *boxed;
|
||||
log::error!("Node graph evaluation reported an error alongside its fallback output: {error:?}");
|
||||
Ok(value)
|
||||
}
|
||||
GPoll::Partial(_) | GPoll::Pending => Err("Node graph evaluation did not complete".into()),
|
||||
GPoll::Error(error) => Err(format!("Node graph evaluation failed: {error:?}").into()),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum FileType {
|
||||
Svg,
|
||||
@@ -28,9 +43,10 @@ pub fn detect_file_type(path: &Path) -> Result<FileType, String> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn export_document(
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn export_document(
|
||||
executor: &DynamicExecutor,
|
||||
wgpu_executor: &wgpu_executor::WgpuExecutor,
|
||||
wgpu_executor: wgpu_executor::WgpuExecutorHandle,
|
||||
output_path: PathBuf,
|
||||
file_type: FileType,
|
||||
scale: f64,
|
||||
@@ -57,7 +73,7 @@ pub async fn export_document(
|
||||
}
|
||||
|
||||
// Execute the graph
|
||||
let result = executor.execute(render_config).await?;
|
||||
let result = execute_to_final(executor, render_config)?;
|
||||
|
||||
// Handle the result based on output type
|
||||
match result {
|
||||
@@ -70,7 +86,7 @@ pub async fn export_document(
|
||||
RenderOutputType::Texture(texture) => {
|
||||
// Convert GPU texture to CPU buffer
|
||||
let gpu_raster = Raster::<GPU>::new_gpu(texture);
|
||||
let cpu_raster: Raster<CPU> = gpu_raster.convert(Footprint::BOUNDLESS, wgpu_executor).await;
|
||||
let cpu_raster: Raster<CPU> = block_on(gpu_raster.convert(Footprint::BOUNDLESS, wgpu_executor.clone()));
|
||||
let (data, width, height) = cpu_raster.to_flat_u8();
|
||||
|
||||
// Encode and write raster image
|
||||
@@ -149,9 +165,9 @@ impl AnimationParams {
|
||||
}
|
||||
|
||||
/// Export an animated GIF by rendering multiple frames at different animation times
|
||||
pub async fn export_gif(
|
||||
pub fn export_gif(
|
||||
executor: &DynamicExecutor,
|
||||
wgpu_executor: &wgpu_executor::WgpuExecutor,
|
||||
wgpu_executor: wgpu_executor::WgpuExecutorHandle,
|
||||
output_path: PathBuf,
|
||||
scale: f64,
|
||||
(width, height): (Option<u32>, Option<u32>),
|
||||
@@ -195,14 +211,14 @@ pub async fn export_gif(
|
||||
}
|
||||
|
||||
// Execute the graph for this frame
|
||||
let result = executor.execute(render_config).await?;
|
||||
let result = execute_to_final(executor, render_config)?;
|
||||
|
||||
// Extract RGBA data from result
|
||||
let (data, img_width, img_height) = match result {
|
||||
TaggedValue::RenderOutput(output) => match output.data {
|
||||
RenderOutputType::Texture(texture) => {
|
||||
let gpu_raster = Raster::<GPU>::new_gpu(texture);
|
||||
let cpu_raster: Raster<CPU> = gpu_raster.convert(Footprint::BOUNDLESS, wgpu_executor).await;
|
||||
let cpu_raster: Raster<CPU> = block_on(gpu_raster.convert(Footprint::BOUNDLESS, wgpu_executor.clone()));
|
||||
cpu_raster.to_flat_u8()
|
||||
}
|
||||
RenderOutputType::Buffer { data, width, height } => (data, width, height),
|
||||
|
||||
@@ -7,13 +7,13 @@ use document_format::{GddV1, GddV1Layout};
|
||||
use fern::colors::{Color, ColoredLevelConfig};
|
||||
use futures::executor::block_on;
|
||||
use graph_craft::application_io::EditorPreferences;
|
||||
use graph_craft::application_io::resource::ResourceRegistry;
|
||||
use graph_craft::application_io::{PlatformApplicationIo, PlatformEditorApi};
|
||||
use graph_craft::document::*;
|
||||
use graph_craft::graphene_compiler::Compiler;
|
||||
use graph_craft::proto::ProtoNetwork;
|
||||
use graph_craft::util::load_network;
|
||||
use graphene_std::application_io::{ApplicationIo, NodeGraphUpdateMessage, NodeGraphUpdateSender};
|
||||
use graphene_std::runtime::{DynGraphRuntime, DynSpawner, GraphRuntime, NoopSpawner, RuntimeHandle};
|
||||
use interpreted_executor::dynamic_executor::DynamicExecutor;
|
||||
use interpreted_executor::util::wrap_network_in_scope;
|
||||
use std::error::Error;
|
||||
@@ -101,8 +101,7 @@ struct GlobalOpts {
|
||||
verbose: u8,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let app = App::parse();
|
||||
|
||||
let log_level = app.global_opts.verbose;
|
||||
@@ -130,9 +129,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let gdd = if is_gdd {
|
||||
let archive = std::fs::read(document_path).map_err(|error| format!("Failed to read document {}: {error}", document_path.display()))?;
|
||||
let container = AnyContainer::Memory(MemoryBackend::new());
|
||||
let gdd = document_format::Gdd::open_from_archive(archive.as_ref(), container, GddV1Layout)
|
||||
.await
|
||||
.map_err(|error| format!("Failed to open document: {error}"))?;
|
||||
let gdd = block_on(document_format::Gdd::open_from_archive(archive.as_ref(), container, GddV1Layout)).map_err(|error| format!("Failed to open document: {error}"))?;
|
||||
Some(gdd)
|
||||
} else {
|
||||
None
|
||||
@@ -140,7 +137,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
if let Command::ExtractLegacyDoc { ref document } = app.command {
|
||||
let Some(gdd) = &gdd else { return Err("ExtractLegacyDoc requires a .gdd document".into()) };
|
||||
let Some(legacy_doc) = gdd.read_legacy_document().await else {
|
||||
let Some(legacy_doc) = block_on(gdd.read_legacy_document()) else {
|
||||
return Err("gdd file did not contain a legacy .graphite document".into());
|
||||
};
|
||||
let mut new_path = document.clone();
|
||||
@@ -153,7 +150,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
// Build the runtime network: from the `.gdd` registry, or by loading a legacy `.graphite` document.
|
||||
let node_network = match &gdd {
|
||||
Some(gdd) => {
|
||||
let declarations = gdd.declarations(gdd).await;
|
||||
let declarations = block_on(gdd.declarations(gdd));
|
||||
let (node_network, _metadata) = gdd.registry().to_runtime_with_metadata(&declarations)?;
|
||||
node_network
|
||||
}
|
||||
@@ -164,7 +161,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
};
|
||||
|
||||
log::info!("Creating GPU context");
|
||||
let mut application_io = PlatformApplicationIo::new().await;
|
||||
let mut application_io = block_on(PlatformApplicationIo::new());
|
||||
if let Some(gdd) = &gdd {
|
||||
application_io.inject_resource_proxy(Box::new(gdd.resource_proxy()));
|
||||
}
|
||||
@@ -176,16 +173,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let application_io_for_api = application_io_arc.clone();
|
||||
|
||||
// Get reference to wgpu executor and clone device handle
|
||||
let wgpu_executor_ref = application_io_arc.gpu_executor().unwrap();
|
||||
let wgpu_executor_ref = wgpu_executor::WgpuExecutorHandle(application_io_arc.gpu_executor_arc().unwrap());
|
||||
let device = wgpu_executor_ref.context().device.clone();
|
||||
|
||||
let preferences = EditorPreferences {
|
||||
max_render_region_size: EditorPreferences::default().max_render_region_size,
|
||||
};
|
||||
let graph_runtime: Arc<DynGraphRuntime> = Arc::new(GraphRuntime::new(Box::new(NoopSpawner) as Box<DynSpawner>));
|
||||
let editor_api = Arc::new(PlatformEditorApi {
|
||||
application_io: Some(application_io_for_api),
|
||||
node_graph_message_sender: Box::new(UpdateLogger {}),
|
||||
editor_preferences: Box::new(preferences),
|
||||
runtime: RuntimeHandle(graph_runtime.clone()),
|
||||
});
|
||||
let proto_graph = compile_graph(node_network, editor_api, gdd.as_ref())?;
|
||||
|
||||
@@ -218,7 +217,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let file_type = export::detect_file_type(&output)?;
|
||||
|
||||
// Create executor
|
||||
let executor = create_executor(proto_graph)?;
|
||||
let executor = create_executor(proto_graph, graph_runtime)?;
|
||||
|
||||
if fps <= 0. {
|
||||
return Err("Fps number must be positive".into());
|
||||
@@ -227,9 +226,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
// Perform export based on file type
|
||||
if file_type == export::FileType::Gif {
|
||||
let animation = export::AnimationParams::new(fps, frames, duration);
|
||||
export::export_gif(&executor, wgpu_executor_ref, output, scale, (width, height), animation).await?;
|
||||
export::export_gif(&executor, wgpu_executor_ref.clone(), output, scale, (width, height), animation)?;
|
||||
} else {
|
||||
export::export_document(&executor, wgpu_executor_ref, output, file_type, scale, (width, height), transparent).await?;
|
||||
export::export_document(&executor, wgpu_executor_ref.clone(), output, file_type, scale, (width, height), transparent)?;
|
||||
}
|
||||
}
|
||||
_ => unreachable!("All other commands should be handled before this match statement is run"),
|
||||
@@ -285,7 +284,8 @@ fn compile_graph(network: NodeNetwork, editor_api: Arc<PlatformEditorApi>, gdd:
|
||||
compiler.compile_single(network).map_err(|x| x.into())
|
||||
}
|
||||
|
||||
fn create_executor(proto_network: ProtoNetwork) -> Result<DynamicExecutor, Box<dyn Error>> {
|
||||
let executor = block_on(DynamicExecutor::new(proto_network)).map_err(|errors| errors.iter().map(|e| format!("{e:?}")).reduce(|acc, e| format!("{acc}\n{e}")).unwrap_or_default())?;
|
||||
fn create_executor(proto_network: ProtoNetwork, runtime: Arc<DynGraphRuntime>) -> Result<DynamicExecutor, Box<dyn Error>> {
|
||||
let mut executor = DynamicExecutor::new(proto_network).map_err(|errors| errors.iter().map(|e| format!("{e:?}")).reduce(|acc, e| format!("{acc}\n{e}")).unwrap_or_default())?;
|
||||
executor.set_runtime(runtime);
|
||||
Ok(executor)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user